2

I am new in Asynchronous Python. I study the behavior of asynchronous iterators. I do everything as written by pep492. But get it RuntimeError: Task got bad yield: 1. Help please understand what i do wrong. On this site, I read about this error but did not understand anything

class Awaitable:
    def __await__(self):
        i = 1
        while i < 3:
            yield i
            print("yield {}".format(i))
            i +=1
        return i

class AsyncIterator:

    def __aiter__(self):
        return self

    async def __anext__(self):
        try:
            value = await Awaitable()
        except StopIteration:
            raise StopAsyncIteration
        return value

i = AsyncIterator().__aiter__()

async def coro():
    while True:
        try:
            row = await i.__anext__()
            print(row)
        except StopAsyncIteration:
            break
        else:
            print(row)

event_loop = asyncio.get_event_loop()
event_loop.run_until_complete(coro())
.............................................................
  File "C:\Users\mykola\Downloads\async_await.py", line 342, in __await__
    yield i
RuntimeError: Task got bad yield: 1

1 Answers1

0

When you run your code with asyncio then asyncio complains.

It expects you to return Futures from that yield.

The language let's you do it. Asyncio complains here.

See this question Precise specification of __await__

Mihai Andrei
  • 1,024
  • 8
  • 11