每PEP-492我试图实现一个异步迭代器,这样我就可以做例如
async for foo in bar:
...
下面是一个简单的例子,与文档中的例子类似,它对实例化和异步迭代进行了非常基本的测试:
import pytest
class TestImplementation:
def __aiter__(self):
return self
async def __anext__(self):
raise StopAsyncIteration
@pytest.mark.asyncio # note use of pytest-asyncio marker
async def test_async_for():
async for _ in TestImplementation():
pass
但是,当我执行测试套件时,我看到:
=================================== FAILURES ===================================
________________________________ test_async_for ________________________________
@pytest.mark.asyncio
async def test_async_for():
> async for _ in TestImplementation():
E TypeError: 'async for' received an invalid object from __aiter__: TestImplementation
...: TypeError
===================== 1 failed, ... passed in 2.89 seconds ======================
为什么我的TestImplementation
看起来无效?据我所知符合协议:
- An object must implement an
__aiter__
method ... returning an asynchronous iterator object.- An asynchronous iterator object must implement an
__anext__
method ... returning an awaitable.- To stop iteration
__anext__
must raise aStopAsyncIteration
exception.
最新发布的Python版本(3.5.1)、py.test
(2.9.2)和pytest-asyncio
(0.4.1)都会失败。
如果你读a little further down the documentation它提到(强调我的):
因此,对于3.5.2(2016/6/27版)之前的版本,文档与如何编写工作异步迭代器有些不协调。3.5.0和3.5.1的固定版本如下:
这是在结束bug #27243时引入的,在data model documentation中更清楚一些,这也建议了一种编写向后兼容代码的方法。
异步迭代器已在Python3.6中实现-请参见PEP-525
那么,为了使用
async for
,您根本不需要执行测试。您只需使用yield
(示例取自PEP-525):然后可以按预期使用
async for
:相关问题 更多 >
编程相关推荐