Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Generator says it doesn't implement __anext__, when it does

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?

like image 699
Jamie Marshall Avatar asked Jan 19 '26 18:01

Jamie Marshall


1 Answers

Calling __aiter__ must return an asynchronous iterator object (which is self in this case). When defined with async def, calling it would return a coroutine resulting in an asynchronous iterator object only after using await.

So in this case the simple fix is to change __aiter__ to a regular method without async:

def __aiter__(self):
    return self

Reference: https://docs.python.org/3.9/reference/datamodel.html#asynchronous-iterators, where also the typical usage of __aiter__ and __anext__ is shown.

like image 62
mkrieger1 Avatar answered Jan 21 '26 07:01

mkrieger1