tractor/examples/parallelism/we_are_processes.py

38 lines
850 B
Python
Raw Permalink Normal View History

2021-01-17 20:42:05 +00:00
"""
Run with a process monitor from a terminal using:
2021-01-18 02:24:43 +00:00
$TERM -e watch -n 0.1 "pstree -a $$" & python examples/parallelism/we_are_processes.py && kill $!
2021-01-17 20:42:05 +00:00
"""
from multiprocessing import cpu_count
import os
import tractor
import trio
async def target():
2021-01-18 02:24:43 +00:00
print(f"Yo, i'm '{tractor.current_actor().name}' "
2021-01-17 20:42:05 +00:00
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__':
2021-01-18 02:24:43 +00:00
try:
trio.run(main)
except Exception:
print('Zombies Contained')