2024-09-19 02:47:59 +00:00
|
|
|
import tractor
|
|
|
|
import trio
|
|
|
|
|
|
|
|
|
|
|
|
log = tractor.log.get_console_log(
|
|
|
|
_root_name='my_app',
|
|
|
|
name='client',
|
|
|
|
)
|
2024-09-19 06:51:33 +00:00
|
|
|
_loglevel: str = 'cancel'
|
2024-09-19 02:47:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def client_main():
|
|
|
|
|
|
|
|
# enable console logging for our custom app's logger
|
|
|
|
tractor.log.get_console_log(
|
2024-09-19 06:51:33 +00:00
|
|
|
level=_loglevel,
|
2024-09-19 02:47:59 +00:00
|
|
|
_root_name='my_app',
|
|
|
|
name='client',
|
|
|
|
)
|
|
|
|
|
2024-09-19 06:51:33 +00:00
|
|
|
# presuming you can get a ref to the target server RPC-ctx func,
|
|
|
|
# pass it directly as our rpc-ctx endpoint below.
|
2024-09-19 02:47:59 +00:00
|
|
|
from server import proxy_request
|
2024-09-19 06:51:33 +00:00
|
|
|
#
|
|
|
|
# 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)`
|
2024-09-19 02:47:59 +00:00
|
|
|
|
|
|
|
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
|
2024-09-19 06:51:33 +00:00
|
|
|
loglevel=_loglevel,
|
2024-09-19 02:47:59 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
# 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!)
|
2024-09-19 06:51:33 +00:00
|
|
|
# tractor.find_actor(
|
|
|
|
# name='web_proxier',
|
|
|
|
# registry_addrs=[('127.0.0.1', 1616)],
|
|
|
|
# ) as portal,
|
|
|
|
|
|
|
|
tractor.wait_for_actor(
|
2024-09-19 02:47:59 +00:00
|
|
|
name='web_proxier',
|
2024-09-19 06:51:33 +00:00
|
|
|
registry_addr=('127.0.0.1', 1616),
|
2024-09-19 02:47:59 +00:00
|
|
|
) 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)
|
|
|
|
|
|
|
|
|
2024-09-19 06:51:33 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
trio.run(client_main)
|