Asyncio.cancel() a cancellation utility as a coroutine [This time with feeling]

I think your argument exactly matches with the above GitHub issue.

async def cancel_and_wait(task, msg=None):
    task.cancel(msg)
    try:
        await task
    except asyncio.CancelledError:
        if asyncio.current_task().cancelling() == 0:
            raise
        else:
            return # this is the only non-exceptional return
    else:
        raise RuntimeError("Cancelled task did not end with an exception")

This seems to be a pretty much repeating pattern along with structured concurrency.
Personally I agree with you in that it would be better to have an instrinsic support for this pattern from the stdlib, but we need to persuade other core developers.

2 Likes