Skip to content

Add an easy and fool proof way to cancel a task #3

@jsbueno

Description

@jsbueno

As it turns out, the apparently simple act of canceling a task would require some carefully crafted code to be made correctly.

check:

https://discuss.python.org/t/asyncio-cancel-a-cancellation-utility-as-a-coroutine-this-time-with-feeling/26304

On the first post by Jason Fried:

# https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.cancel
task.cancel()
try:
    await task
except asyncio.CancelledError:
    print("...")

BUT… What if the await task took some time and something cancelled that task doing the cancelling? It is going to eat the CancelledError which goes against the contract of cancellation. So we need something a little more complicated to tell the difference between a cancellation from below and one coming from above.

It turns out a proper failproof cancel can be part of extraasync, regardless of overlap with Fried's own later project.

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