Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listing available nodes #966

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
72 changes: 72 additions & 0 deletions examples/low-level-api/list-nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
import asyncio
from asyncio import TimeoutError
from datetime import datetime, timezone
import json
import sys
import pathlib
from typing import AsyncIterator, List

from yapapi import props as yp
from yapapi.log import enable_default_logger
from yapapi.props.builder import DemandBuilder
from yapapi.rest import Configuration, Market, Activity, Payment # noqa
from yapapi.rest.market import OfferProposal
from examples import utils

examples_dir = pathlib.Path(__file__).resolve().parent.parent
Copy link
Contributor

@krunch3r76 krunch3r76 May 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

krunch3r here from the community, nice work!

this won't run on my windows install... i observed that from the perspective of python, the examples module is searched from its parent directory. did you mean:

examples_parent_dir = pathlib.Path(__file__).resolve().parent.parent.parent

and to place it above line 15 or is this just extra code? ♥️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you want to achieve. I just copied this part of code from list-offers.py example.

Copy link
Contributor

@krunch3r76 krunch3r76 Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as is, this will emit: ModuleNotFoundError: No module named 'examples' on account of the import statement from examples preceding the module search path modification.

ironically i pulled a fix for this issue to list-offers.py some time ago and you must have been looking at another branch when you copied(, and in fact i see shade of blue has merged master into this branch to bring it up to date recently):
aa86ed3

i was originally suggesting here instead:

examples_dir = pathlib.Path(__file__).resolve().parent.parent.parent                                
sys.path.append(str(examples_dir))                                                                  
from examples import utils 

but i think the modification i made to list-offers.py (see link to commit hash above) is a little more clean/concise

sys.path.append(str(examples_dir))


class NodeInfo(object):
def __init__(self, offer: OfferProposal):
self.node_id = offer.issuer
self.node_name = offer.props["golem.node.id.name"]


async def list_offers(conf: Configuration, subnet_tag: str) -> AsyncIterator[OfferProposal]:
async with conf.market() as client:
market_api = Market(client)
dbuild = DemandBuilder()
dbuild.add(yp.NodeInfo(name="Scanning Node", subnet_tag=subnet_tag))
dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc)))

async with market_api.subscribe(dbuild.properties, dbuild.constraints) as subscription:
async for event in subscription.events():
yield event


async def list_nodes(conf: Configuration, subnet_tag: str) -> AsyncIterator[List[NodeInfo]]:
async for offer in list_offers(conf, subnet_tag):
yield NodeInfo(offer)


async def print_nodes(conf: Configuration, subnet_tag: str):
async for node in list_nodes(conf, subnet_tag=subnet_tag):
print(f"{node.node_id} {node.node_name}")


def main():
parser = utils.build_parser("List Nodes")
args = parser.parse_args()

nieznanysprawiciel marked this conversation as resolved.
Show resolved Hide resolved
subnet = args.subnet_tag
sys.stderr.write(f"Using subnet: {utils.TEXT_COLOR_YELLOW}{subnet}{utils.TEXT_COLOR_DEFAULT}\n")

enable_default_logger()
try:
asyncio.get_event_loop().run_until_complete(
asyncio.wait_for(
print_nodes(
Configuration(),
subnet_tag=subnet,
),
timeout=4,
nieznanysprawiciel marked this conversation as resolved.
Show resolved Hide resolved
)
)
except TimeoutError:
pass


if __name__ == "__main__":
main()