From 582eda4afd12e4666fee2591a7bb37a315cff395 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 17 Jan 2021 15:42:05 -0500 Subject: [PATCH] Add concise readme example --- examples/parallelism/we_are_processes.py | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/parallelism/we_are_processes.py diff --git a/examples/parallelism/we_are_processes.py b/examples/parallelism/we_are_processes.py new file mode 100644 index 0000000..ac4b594 --- /dev/null +++ b/examples/parallelism/we_are_processes.py @@ -0,0 +1,34 @@ +""" +Run with a process monitor from a terminal using: +$TERM -e watch -n 0.1 "pstree -a $$" & python examples/parallelism/we_are_processes.py || kill $! + +""" +from multiprocessing import cpu_count +import os + +import tractor +import trio + + +async def target(): + print(f"Yo, i'm {tractor.current_actor().name} " + f"running in pid {os.getpid()}") + await trio.sleep_forever() + + +async def main(): + + async with tractor.open_nursery() as n: + + for i in range(cpu_count()): + await n.run_in_actor(target, name=f'worker_{i}') + + print('This process tree will self-destruct in 1 sec...') + await trio.sleep(1) + + # you could have done this yourself + raise Exception('Self Destructed') + + +if __name__ == '__main__': + trio.run(main)