tractor/examples/actor_spawning_and_causalit...

35 lines
923 B
Python
Raw Permalink Normal View History

2021-02-24 18:39:14 +00:00
import trio
import tractor
2021-04-27 13:14:08 +00:00
async def movie_theatre_question():
"""A question asked in a dark theatre, in a tangent
(errr, I mean different) process.
"""
return 'have you ever seen a portal?'
async def main():
"""The main ``tractor`` routine.
"""
async with tractor.open_nursery() as n:
portal = await n.start_actor(
'frank',
# enable the actor to run funcs from this current module
2021-02-24 23:46:33 +00:00
enable_modules=[__name__],
)
print(await portal.run(movie_theatre_question))
# call the subactor a 2nd time
print(await portal.run(movie_theatre_question))
# the async with will block here indefinitely waiting
# for our actor "frank" to complete, but since it's an
# "outlive_main" actor it will never end until cancelled
await portal.cancel_actor()
if __name__ == '__main__':
2021-02-24 18:39:14 +00:00
trio.run(main)