Add concise readme example

eg_worker_poolz
Tyler Goodlet 2021-01-17 15:42:05 -05:00
parent 57a24cdcf8
commit da8c8c1773
1 changed files with 34 additions and 0 deletions

View File

@ -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)