Issue
First time using the Aync Generators. I'm using Python 3.9.
This is my implementation:
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())
Confusingly this is the result I keep getting:
/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 <module>
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
Clearly I do implement __anext__
. What's the hold up?
Solution
__aiter__
must be a regular method, not an async
method:
def __aiter__(self):
return self
See: https://docs.python.org/3.9/reference/datamodel.html#asynchronous-iterators
Answered By - mkrieger1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.