[Python-Dev] asyncio: how to interrupt an async def w/ finally: ( e.g. Condition.wait() )

Matthias Urlichs matthias at urlichs.de
Sun Dec 20 00:29:40 EST 2015


On 20.12.2015 01:26, Kevin Conway wrote:
> async def coroutine():
>     try:
>         await Awaitable()
>         await Awaitable()
>     finally:
>         print('finally')
Try adding another "await Awaitable()" after the "finally:".

I have to take back my "doesn't print an error" comment, however;
there's another reference to the Condition.wait() generator (the task
asyncio.wait() creates to wrap the generator in), and the "Task was
destroyed but it is pending!" message got delayed sufficiently that I
missed it. (Dying test cases tend to spew many of these.)

Testcase:

    import asyncio
    import gc
    cond = asyncio.Condition()
    loop = asyncio.get_event_loop()

    async def main():
        async with cond:
            # asyncio.wait() does this, if we don't
            w = asyncio.ensure_future(cond.wait())
            await asyncio.wait([w],timeout=1)
            print(gc.get_referrers(w))
    loop.run_until_complete(main())

Time to refactor my code to do the wait/timeout outside the "async with
cond".

-- 
-- Matthias Urlichs



More information about the Python-Dev mailing list