Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

The documentation on task cancellation is unclear #253

@realazthat

Description

@realazthat

Here is an example:

@asyncio.coroutine
def something(sleep,marker):

    try:
        print ('something() is sleeping for %s  seconds!' % sleep,flush=True)
        yield from asyncio.sleep(sleep)
    finally:
        print ('cleaning something()!',flush=True)
        marker['cleaned'] = True

@asyncio.coroutine
def test_session2():
    marker = {'cleaned': False}
    yield from asyncio.wait_for(something(5, marker), timeout=10)
    #here something is cleaned up

    try:
        marker = {'cleaned': False}
        yield from asyncio.wait_for(something(10, marker), timeout=5)
    except asyncio.TimeoutError:
        print ('something() has timed out')
        pass
    #here something is not cancelled yet ... it cancels later.
    #intuitively, you'd think that wait_for would throw after something() is actually cancelled
    print ('is something cleaned up?', ('YES!' if marker['cleaned'] else 'NO!'))
    print ('sleeping a bit')
    yield from asyncio.sleep(1)
    print ('is something cleaned up?', ('YES!' if marker['cleaned'] else 'NO!'))

loop = asyncio.get_event_loop()

loop.run_until_complete(test_session2())

And the output:

$ python3 asyncio.wait_for.test.py
something() is sleeping for 5  seconds!
cleaning something()!
something() is sleeping for 10  seconds!
something() has timed out
is something cleaned up? NO!
sleeping a bit
cleaning something()!
is something cleaned up? YES!

So wait_for() throws TimeoutError, but the coroutine is not yet cancelled.

Basically, I think that the task should first be cancelled, and then wait_for() should return.

OR.

It should be more explicitly documented that the task will cancel, but not necessarily before wait_for() raises an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions