[Async-sig] Additive merging of async generators

James Stidard jamesstidard at gmail.com
Wed Jul 18 09:01:41 EDT 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20180718/19f2eaee/attachment.html>


More information about the Async-sig mailing list