From ace1b1312c953166f4507a4802e9b6af1debd77d Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 2 Aug 2021 21:49:15 -0400 Subject: [PATCH] Terminate async gen example caller to avoid (benign) errors in console output --- examples/asynchronous_generators.py | 40 +++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/asynchronous_generators.py b/examples/asynchronous_generators.py index 99b0a13..b037b08 100644 --- a/examples/asynchronous_generators.py +++ b/examples/asynchronous_generators.py @@ -1,39 +1,41 @@ +from typing import AsyncIterator from itertools import repeat + import trio import tractor -tractor.log.get_console_log("INFO") +async def stream_forever() -> AsyncIterator[int]: -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 + # each yielded value is sent over the ``Channel`` to the parent actor yield i await trio.sleep(0.01) async def main(): - # stream for at most 1 seconds - with trio.move_on_after(1) as cancel_scope: + async with tractor.open_nursery() as n: - async with tractor.open_nursery() as n: + portal = await n.start_actor( + 'donny', + enable_modules=[__name__], + ) - portal = await n.start_actor( - 'donny', - enable_modules=[__name__], - ) + # this async for loop streams values from the above + # async generator running in a separate process + async with portal.open_stream_from(stream_forever) as stream: + count = 0 + async for letter in stream: + print(letter) + count += 1 - # this async for loop streams values from the above - # async generator running in a separate process - async with portal.open_stream_from(stream_forever) as stream: - async for letter in stream: - print(letter) + if count > 50: + break - # we support trio's cancellation system - assert cancel_scope.cancelled_caught - assert n.cancelled + print('stream terminated') + + await portal.cancel_actor() if __name__ == '__main__':