I'm looking to be able to yield from a number of async coroutines. Asyncio's as_completed is kind of close to what I'm looking for (i.e. I want any of the coroutines to be able to yield at any time back to the caller and then continue), but that only seems to allow regular coroutines with a single return.
Here's what I have so far:
import asyncio async def test(id_): print(f'{id_} sleeping') await asyncio.sleep(id_) return id_ async def test_gen(id_): count = 0 while True: print(f'{id_} sleeping') await asyncio.sleep(id_) yield id_ count += 1 if count > 5: return async def main(): runs = [test(i) for i in range(3)] for i in asyncio.as_completed(runs): i = await i print(f'{i} yielded') if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close() Replacing runs = [test(i) for i in range(3)] with runs = [test_gen(i) for i in range(3)] and for for i in asyncio.as_completed(runs) to iterate on each yield is what I'm after.
Is this possible to express in Python and are there any third party maybe that give you more options then the standard library for coroutine process flow?
Thanks