You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Playing with delays and debugging prints shows that this code does what is intended: it waits for tasks already obtained from stream_of_tasks to complete and for more tasks to be generated by stream_of_tasks at the same time. Basically, it's asyncio.wait() that supports async collections — or rather, some ugly hack. It doesn't play well with exceptions and can only wait for the completion of all tasks at once. Still, it's somewhat useful for tasks that has to be completed, but don't return a result and have no reasonable place to be awaited for.
Is there a better way to do this with aiostream, or maybe the problem it tries to solve (awaiting all tasks from an awaitable collection) can be evaded entirely?
The text was updated successfully, but these errors were encountered:
I can think of three different ways to implement the feature you're looking for:
importrandomimportasynciofromaiostreamimportstream, pipeasyncdefdo_something(x):
print(f'Doing something for {x:.2f} s...')
awaitasyncio.sleep(x)
print(f'Done something for {x:.2f} s...')
asyncdefproduce_coros():
for_inrange(5):
awaitasyncio.sleep(random.random())
yielddo_something(random.random())
asyncdef_await(arg):
returnawaitargasyncdefmain():
# Version 1awaitstream.combine.amap(produce_coros(), lambdax: x)
print()
# Version 2awaitstream.map(produce_coros(), _await)
print()
# Version 3awaitstream.flatmap(produce_coros(), stream.just)
print()
if__name__=='__main__':
loop=asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
The last one is probably the simplest, although flatmap is meant to be an advanced operator. Maybe the library is missing an extra helper, but I can't really figure it out. Maybe something like asyncio.as_completed:
Playing with delays and debugging prints shows that this code does what is intended: it waits for tasks already obtained from
stream_of_tasks
to complete and for more tasks to be generated bystream_of_tasks
at the same time. Basically, it'sasyncio.wait()
that supports async collections — or rather, some ugly hack. It doesn't play well with exceptions and can only wait for the completion of all tasks at once. Still, it's somewhat useful for tasks that has to be completed, but don't return a result and have no reasonable place to be awaited for.Is there a better way to do this with
aiostream
, or maybe the problem it tries to solve (awaiting all tasks from an awaitable collection) can be evaded entirely?The text was updated successfully, but these errors were encountered: