[issue30048] If a task is canceled at the right moment, the cancellation is ignored

INADA Naoki report at bugs.python.org
Wed Apr 12 20:19:13 EDT 2017


INADA Naoki added the comment:

> The problem is that the task doesn't catch CancelledError, yet it disappears.

The problem is CancelledError is not raised, even it's thrown.
Task can't catch exception not raised.  See below example which demonstrates how task works.

---
from asyncio import CancelledError

cancelled = False

def coro():
    global cancelled
    print(1)
    yield (1,)
    print("cancel")
    cancelled = True
    #print(2)
    #yield (2,)  # uncomment this line makes cancel success.

c = coro()

while True:
    try:
        if cancelled:
            r = c.throw(CancelledError)
        else:
            r = c.send(None)
    except StopIteration:
        print("end")
        break
    except CancelledError:
        print("cancelled")
        break

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30048>
_______________________________________


More information about the Python-bugs-list mailing list