2017-03-06 85 views
0

我正在学习Python的相对较新的异步功能。我发现这在PEP 492来自Python 3的TypeError异步循环

The following is a utility class that transforms a regular iterable to an asynchronous one. While this is not a very useful thing to do, the code illustrates the relationship between regular and asynchronous iterators.

class AsyncIteratorWrapper: 
    def __init__(self, obj): 
     self._it = iter(obj) 

    def __aiter__(self): 
     return self 

    async def __anext__(self): 
     try: 
      value = next(self._it) 
     except StopIteration: 
      raise StopAsyncIteration 
     return value 

async for letter in AsyncIteratorWrapper("abc"): 
    print(letter) 

我试图通过增加给定async for循环的功能,然后调用,使用一个事件循环运行该代码。

完整的示例代码(在解释器中运行):

class AsyncIteratorWrapper: 
    def __init__(self, obj): 
     self._it = iter(obj) 
    def __aiter__(self): 
     return self 
    async def __anext__(self): 
     try: 
      value = next(self._it) 
     except StopIteration: 
      raise StopAsyncIteration 
     return value 

async def aprint(str): 
    async for letter in AsyncIteratorWrapper(str): 
    print(letter) 

import asyncio 
loop = asyncio.get_event_loop() 
co = aprint("abcde") 
loop.run_until_complete(co) 

不过,我得到一个错误:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/opt/rh/rh-python35/root/usr/lib64/python3.5/asyncio/base_events.py", line 337, in run_until_complete 
    return future.result() 
    File "/opt/rh/rh-python35/root/usr/lib64/python3.5/asyncio/futures.py", line 274, in result 
    raise self._exception 
    File "/opt/rh/rh-python35/root/usr/lib64/python3.5/asyncio/tasks.py", line 239, in _step 
    result = coro.send(None) 
    File "<stdin>", line 2, in aprint 
TypeError: 'async for' received an invalid object from __aiter__: AsyncIteratorWrapper 

我在做什么错?这个例子如何解决?我感到有点惊讶,PEP中的代码失败了。

我正在使用python版本3.5.1。

+0

它正在处理'python3.6' – sobolevn

+1

也可以使用3.5 - 查看更改日志我发现“版本3.5.2中已更改:从CPython 3.5.2开始,__aiter__可以直接返回异步迭代器。” – Orange

+0

对于''__aiter__'中的几个版本假设是异步的,但是随后symantics变成直接返回异步迭代,所以只需更新到更新版本的python,你应该没问题。 –

回答

0

您使用的代码适用于python 3.5.2+。

从Python 3.5.2 __aiter__可以直接返回异步迭代器。 More here

您收到的错误是由于较旧的python(3.5.1),因此它返回了错误的类型。