tractor/examples/multihost/client.py

64 lines
1.8 KiB
Python

import tractor
import trio
log = tractor.log.get_console_log(
_root_name='my_app',
name='client',
)
_loglevel: str = 'cancel'
async def client_main():
# enable console logging for our custom app's logger
tractor.log.get_console_log(
level=_loglevel,
_root_name='my_app',
name='client',
)
# presuming you can get a ref to the target server RPC-ctx func,
# pass it directly as our rpc-ctx endpoint below.
from server import proxy_request
#
# NOTE, see he equiv note in `server.py` explaining why this will
# render more or less to `'server:proxy_request'` according to
# `tractor.msg.NamespacePath.from_ref(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
loglevel=_loglevel,
),
# 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,
tractor.wait_for_actor(
name='web_proxier',
registry_addr=('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)
if __name__ == '__main__':
trio.run(client_main)