tractor/examples/parallelism/single_func.py

45 lines
832 B
Python
Raw Normal View History

2021-02-27 19:21:27 +00:00
"""
Run with a process monitor from a terminal using::
$TERM -e watch -n 0.1 "pstree -a $$" \
& python examples/parallelism/single_func.py \
&& kill $!
"""
import os
import tractor
import trio
async def burn_cpu() -> int:
2021-02-27 19:21:27 +00:00
pid = os.getpid()
# burn a core @ ~ 50kHz
for _ in range(50000):
await trio.sleep(1/50000/50)
return os.getpid()
async def main() -> None:
2021-02-27 19:21:27 +00:00
n: tractor.ActorNursery
2021-02-27 19:21:27 +00:00
async with tractor.open_nursery() as n:
portal: tractor.Portal = await n.run_in_actor(burn_cpu)
2021-02-27 19:21:27 +00:00
# burn rubber in the parent too
await burn_cpu()
# wait on result from target function
pid: int = await portal.wait_for_result()
2021-02-27 19:21:27 +00:00
# end of nursery block
print(f"Collected subproc {pid}")
if __name__ == '__main__':
trio.run(main)