带有asyncio的惰性迭代器(生成器)

时间:2016-05-17 15:16:38

标签: python asynchronous python-asyncio

我有一个像这样的阻塞,非异步代码:

def f():
    def inner():
        while True:
            yield read()
    return inner()

使用此代码,调用者可以选择何时停止该函数来生成数据。如何将此更改为异步?此解决方案不起作用:

async def f():
    async def inner():
        while True:
            yield await coroutine_read()
    return inner()

...因为yield无法在async def函数中使用。如果我从async签名中删除inner(),我将无法再使用await

1 个答案:

答案 0 :(得分:7)

<强> UPD:

从Python 3.6开始,我们有asynchronous generators并且能够直接在协同程序中使用yield

如上所述,您无法在yield个功能中使用async。如果您要创建coroutine-generator,则必须使用__aiter____anext__魔术方法手动执行此操作:

import asyncio


# `coroutine_read()` generates some data:
i = 0
async def coroutine_read():
    global i
    i += 1
    await asyncio.sleep(i)
    return i


# `f()` is asynchronous iterator.
# Since we don't raise `StopAsyncIteration` 
# it works "like" `while True`, until we manually break.
class f:
    async def __aiter__(self):
        return self

    async def __anext__(self):
        return await coroutine_read()


# Use f() as asynchronous iterator with `async for`:
async def main():
    async for i in f():
        print(i)
        if i >= 3:
            break


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

输出:

1
2
3
[Finished in 6.2s]

您可能还希望other post看到StopAsyncIteration使用的地方。