You are not appending to the list that's being iterated ;)
tasks is an async generator or possibly a custom object that overrides aiter, anext, etc.
I'd say look at internals of aiostream's merge, it should not be too hard to extend perhaps
_______________________________________________Hi,
I was wondering if anyone has had experience implementing a similar pattern to this or has alternative suggestions of how to achieve it.
I have a bunch of async generators which I’d like to be able to merge into a single async generator I can iterate over. I found Vincent’s
aiostream
library which gives me this without too much effort:from asyncio import sleep, run from aiostream.stream import merge async def go(): yield 0 await sleep(1) yield 50 await sleep(1) yield 100 async def main(): tasks = merge(go(), go(), go()) async for v in tasks: print(v) if __name__ == '__main__': run(main())
However, I also would like to be able to add additional tasks into the list once I’ve started iterating the list so something more akin to:
from asyncio import sleep, run from aiostream.stream import merge async def go(): yield 0 await sleep(1) yield 50 await sleep(1) yield 100 async def main(): tasks = merge(go(), go(), go()) async for v in tasks: If v == 50: tasks.merge(go()) print(v) if __name__ == '__main__': run(main())
Has anyone been able to achieve something like this?
p.s. I know appending to a list you’re iterating is bad practice, I assume the same would be true modifying this stream object, but think the example illustrates what I’m trying to achieve.
Thanks,
James
Async-sig mailing list
Async-sig@python.org
https://mail.python.org/mailman/listinfo/async-sig
Code of Conduct: https://www.python.org/psf/codeofconduct/