2021-11-02 19:37:36 +00:00
|
|
|
|
|
|
|
import trio
|
|
|
|
import tractor
|
|
|
|
|
|
|
|
|
2025-03-03 22:55:07 +00:00
|
|
|
async def sleepy_jane() -> None:
|
|
|
|
uid: tuple = tractor.current_actor().uid
|
2021-11-02 19:37:36 +00:00
|
|
|
print(f'Yo i am actor {uid}')
|
|
|
|
await trio.sleep_forever()
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
'''
|
2025-03-03 22:55:07 +00:00
|
|
|
Spawn a flat actor cluster, with one process per detected core.
|
2021-11-02 19:37:36 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
portal_map: dict[str, tractor.Portal]
|
|
|
|
|
|
|
|
# look at this hip new syntax!
|
|
|
|
async with (
|
|
|
|
|
|
|
|
tractor.open_actor_cluster(
|
|
|
|
modules=[__name__]
|
|
|
|
) as portal_map,
|
|
|
|
|
2025-03-03 22:55:07 +00:00
|
|
|
trio.open_nursery(
|
|
|
|
strict_exception_groups=False,
|
|
|
|
) as tn,
|
2021-11-02 19:37:36 +00:00
|
|
|
):
|
|
|
|
|
|
|
|
for (name, portal) in portal_map.items():
|
2025-03-03 22:55:07 +00:00
|
|
|
tn.start_soon(
|
|
|
|
portal.run,
|
|
|
|
sleepy_jane,
|
|
|
|
)
|
2021-11-02 19:37:36 +00:00
|
|
|
|
|
|
|
await trio.sleep(0.5)
|
|
|
|
|
|
|
|
# kill the cluster with a cancel
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
trio.run(main)
|
|
|
|
except KeyboardInterrupt:
|
2025-03-03 22:55:07 +00:00
|
|
|
print('trio cancelled by KBI')
|