Async Generator говорит, что не реализует __anext__, хотя он это делаетPython

Программы на Python
Anonymous
Async Generator говорит, что не реализует __anext__, хотя он это делает

Сообщение Anonymous »

Впервые использую асинхронные генераторы. Я использую Python 3.9.
Это моя реализация:

Код: Выделить всё

import asyncio

class SubEventStream():
def __init__(self) -> None:
self.queue = asyncio.Queue()
return

async def __aiter__(self):
return self

async def __anext__(self):
return await self.pop()

async def append(self, request):
return await self.queue.put(request)

async def pop(self):
r = await self.queue.get()
self.queue.task_done()
return r

def create_append_tasks(ls, q):
return [
asyncio.create_task(q.append(i))
for i in ls
]

async def append_tasks(q):
tasks = create_append_tasks(('a', 'b', 'c', 'd', 'e'), q)
return await asyncio.gather(*tasks)

async def run():
q = SubEventStream()
await append_tasks(q)

async for v in q:
print(v)

asyncio.run(run())
Как ни странно, я продолжаю получать вот такой результат:

Код: Выделить всё

/tmp/tmp.ie3Dj7Q9hn/test.py:37: RuntimeWarning: coroutine 'SubEventStream.__aiter__' was never awaited
async for v in q:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
File "test.py", line 40, in 
asyncio.run(run())
File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "test.py", line 37, in run
async for v in q:
TypeError: 'async for' received an object from __aiter__ that does not implement __anext__: coroutine
Очевидно, я реализую __anext__. Что за задержка?


Подробнее здесь: https://stackoverflow.com/questions/730 ... en-it-does

Вернуться в «Python»