tractor/examples/multihost/client.py

51 lines
1.3 KiB
Python
Raw Normal View History

import tractor
import trio
log = tractor.log.get_console_log(
_root_name='my_app',
name='client',
)
async def client_main():
# enable console logging for our custom app's logger
tractor.log.get_console_log(
level='info',
_root_name='my_app',
name='client',
)
# presuming you can get a ref to the target server RPC-ctx func
from server import proxy_request
async with (
tractor.open_root_actor(
name='web_requester',
registry_addrs=[('127.0.0.1', 1616)],
enable_modules=[], # since this isn't a service actor
),
# use discovery api to find the server actor on your net
# (NOTE, in which case the below registry addr would have to
# be the public IP of that host!)
tractor.find_actor(
name='web_proxier',
registry_addrs=[('127.0.0.1', 1616)],
) as portal,
# open an RPC context with the remote actor, thus spawning
# a new task implemented as the function defined in the
# server code.
portal.open_context(
proxy_request,
address='https://github.com',
) as (ctx, first),
):
resp: dict = await ctx.result()
print(resp)
trio.run(client_main)