Terminate async gen example caller to avoid (benign) errors in console output
parent
7431e8ea01
commit
ace1b1312c
|
@ -1,39 +1,41 @@
|
||||||
|
from typing import AsyncIterator
|
||||||
from itertools import repeat
|
from itertools import repeat
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
import tractor
|
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"):
|
for i in repeat("I can see these little future bubble things"):
|
||||||
# each yielded value is sent over the ``Channel`` to the
|
# each yielded value is sent over the ``Channel`` to the parent actor
|
||||||
# parent actor
|
|
||||||
yield i
|
yield i
|
||||||
await trio.sleep(0.01)
|
await trio.sleep(0.01)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
# stream for at most 1 seconds
|
async with tractor.open_nursery() as n:
|
||||||
with trio.move_on_after(1) as cancel_scope:
|
|
||||||
|
|
||||||
async with tractor.open_nursery() as n:
|
portal = await n.start_actor(
|
||||||
|
'donny',
|
||||||
|
enable_modules=[__name__],
|
||||||
|
)
|
||||||
|
|
||||||
portal = await n.start_actor(
|
# this async for loop streams values from the above
|
||||||
'donny',
|
# async generator running in a separate process
|
||||||
enable_modules=[__name__],
|
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
|
if count > 50:
|
||||||
# async generator running in a separate process
|
break
|
||||||
async with portal.open_stream_from(stream_forever) as stream:
|
|
||||||
async for letter in stream:
|
|
||||||
print(letter)
|
|
||||||
|
|
||||||
# we support trio's cancellation system
|
print('stream terminated')
|
||||||
assert cancel_scope.cancelled_caught
|
|
||||||
assert n.cancelled
|
await portal.cancel_actor()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue