asyncio awaitable object

ast nomail at com.invalid
Fri Dec 8 04:48:48 EST 2017


"ast" <nomail at com.invalid> a écrit dans le message de news:5a2a568c$0$3699$426a74cc at news.free.fr...

I made some experiment.

It seems that the iterator shall provide None values, an other value
raises an exception: "RuntimeError: Task got bad yield: 1"

and in instruction "res = await obj", res got the StopIteration exception
value

See my test program and output.

import asyncio

class Test:

    def __init__(self):
        self.i = 0
    def __await__(self):
        return self
    def __iter__(self):
        return self
    def __next__(self):
        if self.i < 5:
            self.i += 1
            return None
        else:
            raise StopIteration(11)

test = Test()

async def coro1():
    print("Enter coro1")
    res = await test
    print("end of coro1, res= ", res)

async def coro2():
    print("Enter coro2")
    for i in range(8):
        print("in coro2")
        await asyncio.sleep(0)

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait([coro1(), coro2()]))

========== RESTART ==========
Enter coro1
Enter coro2
in coro2
in coro2
in coro2
in coro2
in coro2
end of coro1, res=  11
in coro2
in coro2
in coro2 




More information about the Python-list mailing list