asyncio as_yielded from async generators

后端 未结 1 1799
独厮守ぢ
独厮守ぢ 2021-02-20 13:31

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 corout

相关标签:
1条回答
  • 2021-02-20 13:43

    You can use aiostream.stream.merge:

    from aiostream import stream
    
    async def main():
        runs = [test_gen(i) for i in range(3)]
        async for x in stream.merge(*runs):
            print(f'{x} yielded')
    

    Run it in a safe context to make sure the generators are cleaned up properly after the iteration:

    async def main():
        runs = [test_gen(i) for i in range(3)]
        merged = stream.merge(*runs)
        async with merged.stream() as streamer:
            async for x in streamer:
                print(f'{x} yielded')
    

    Or make it more compact using pipes:

    from aiostream import stream, pipe
    
    async def main():
        runs = [test_gen(i) for i in range(3)]
        await (stream.merge(*runs) | pipe.print('{} yielded'))
    

    More examples in the documentation.


    Adressing @nirvana-msu comment

    It is possible to identify the generator that yielded a given value by preparing sources accordingly:

    async def main():
        runs = [test_gen(i) for i in range(3)]
        sources = [stream.map(xs, lambda x: (i, x)) for i, xs in enumerate(runs)]
        async for i, x in stream.merge(*sources):
            print(f'ID {i}: {x}')
    
    0 讨论(0)
提交回复
热议问题