From 4cbb8641de12474db9f2f4c157833b5a08f65e07 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 2 Nov 2021 15:37:36 -0400 Subject: [PATCH] Add an `open_actor_cluster()` usage example --- examples/quick_cluster.py | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/quick_cluster.py diff --git a/examples/quick_cluster.py b/examples/quick_cluster.py new file mode 100644 index 0000000..7ced773 --- /dev/null +++ b/examples/quick_cluster.py @@ -0,0 +1,50 @@ +from functools import partial + +import trio +import tractor + + +async def sleepy_jane(): + uid = tractor.current_actor().uid + print(f'Yo i am actor {uid}') + await trio.sleep_forever() + + +async def main(): + ''' + Spawn a flat actor cluster, with one process per + detected core. + + ''' + portal_map: dict[str, tractor.Portal] + results: dict[str, str] + + # look at this hip new syntax! + async with ( + + tractor.open_actor_cluster( + modules=[__name__] + ) as portal_map, + + trio.open_nursery() as n, + ): + + for (name, portal) in portal_map.items(): + n.start_soon( + partial( + portal.run, + sleepy_jane, + ) + ) + + await trio.sleep(0.5) + + # kill the cluster with a cancel + raise KeyboardInterrupt + + +if __name__ == '__main__': + try: + trio.run(main) + except KeyboardInterrupt: + pass