2020-02-09 07:01:39 +00:00
|
|
|
from itertools import repeat
|
|
|
|
import trio
|
|
|
|
import tractor
|
|
|
|
|
|
|
|
tractor.log.get_console_log("INFO")
|
|
|
|
|
|
|
|
|
|
|
|
async def stream_forever():
|
|
|
|
for i in repeat("I can see these little future bubble things"):
|
|
|
|
# each yielded value is sent over the ``Channel`` to the
|
|
|
|
# parent actor
|
|
|
|
yield i
|
|
|
|
await trio.sleep(0.01)
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2021-02-24 18:39:14 +00:00
|
|
|
|
2020-02-09 07:01:39 +00:00
|
|
|
# stream for at most 1 seconds
|
|
|
|
with trio.move_on_after(1) as cancel_scope:
|
2021-02-24 18:39:14 +00:00
|
|
|
|
2020-02-09 07:01:39 +00:00
|
|
|
async with tractor.open_nursery() as n:
|
2021-02-24 18:39:14 +00:00
|
|
|
|
2020-02-09 07:01:39 +00:00
|
|
|
portal = await n.start_actor(
|
2021-02-24 18:39:14 +00:00
|
|
|
'donny',
|
2020-02-09 07:01:39 +00:00
|
|
|
rpc_module_paths=[__name__],
|
|
|
|
)
|
|
|
|
|
|
|
|
# this async for loop streams values from the above
|
|
|
|
# async generator running in a separate process
|
2021-04-28 15:54:56 +00:00
|
|
|
async with portal.open_stream_from(stream_forever) as stream:
|
|
|
|
async for letter in stream:
|
|
|
|
print(letter)
|
2020-02-09 07:01:39 +00:00
|
|
|
|
|
|
|
# we support trio's cancellation system
|
|
|
|
assert cancel_scope.cancelled_caught
|
|
|
|
assert n.cancelled
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-02-24 18:39:14 +00:00
|
|
|
trio.run(main)
|