If I have a coroutine which runs a task which should not be cancelled, I will wrap that task in asyncio.shield()
.
It seems the behavior of cancel
and shield
is not what I would expect. If I have a task wrapped in shield
and I cancel it, the await
-ing coroutine returns from that await
statement immediately rather than awaiting for the task to finish as shield
would suggest. Additionally, the task that was run with shield
continues to run but its future is now cancelled an not await
-able.
From the docs:
except that if the coroutine containing it is cancelled, the Task running in something() is not cancelled. From the point of view of something(), the cancellation did not happen. Although its caller is still cancelled, so the “await” expression still raises a CancelledError.
These docs do not imply strongly that the caller is cancelled potentially before the callee finishes, which is the heart of my issue.
What is the proper method to shield
a task from cancellation and then wait for it to complete before returning.
It would make more sense if asyncio.shield()
raised the asyncio.CancelledError
after the await
-ed task has completed, but obviously there is some other idea going on here that I don't understand.
Here is a simple example:
import asyncio
async def count(n):
for i in range(n):
print(i)
await asyncio.sleep(1)
async def t():
try:
await asyncio.shield(count(5))
except asyncio.CancelledError:
print('This gets called at 3, not 5')
return 42
async def c(ft):
await asyncio.sleep(3)
ft.cancel()
async def m():
ft = asyncio.ensure_future(t())
ct = asyncio.ensure_future(c(ft))
r = await ft
print(r)
loop = asyncio.get_event_loop()
loop.run_until_complete(m())
# Running loop forever continues to run shielded task
# but I'd rather not do that
#loop.run_forever()
A Task is created and scheduled for its execution through the asyncio. create_task() function. Once scheduled, a Task can be requested for cancellation through task. cancel() method.
The asyncio. wait() function has the following parameters: aws is iterable of awaitable objects that you want to run concurrently. timeout (either int or float ) specifies a maximum number of seconds to wait before returning the result. return_when indicates when the function should return.
To run an async function (coroutine) you have to call it using an Event Loop. Event Loops: You can think of Event Loop as functions to run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Example 1: Event Loop example to run async Function to run a single async function: Python3.
The asyncio. sleep() method suspends the execution of a coroutine. Coroutines voluntarily yield CPU leading to co-operative multitasking through the await keyword.
It seems the behavior of
cancel
andshield
is not what I would expect. If I have a task wrapped inshield
and I cancel it, theawait
-ing coroutine returns from thatawait
statement immediately rather than awaiting for the task to finish asshield
would suggest. Additionally, the task that was run withshield
continues to run but its future is now cancelled an notawait
-able.
Conceptually shield
is like a bullet-proof vest that absorbs the bullet and protects the wearer, but is itself destroyed by the impact. shield
absorbs the cancellation, and reports itself as canceled, raising a CancelledError
when asked for result, but allows the protected task to continue running. (Artemiy's answer explains the implementation.)
Cancellation of the future returned by shield
could have been implemented differently, e.g. by completely ignoring the cancel request. The current approach ensures that the cancellation "succeeds", i.e. that the canceller can't tell that the cancellation was in fact circumvented. This is by design, and it makes the cancellation mechanism more consistent on the whole.
What is the proper method to shield a task from cancellation and then wait for it to complete before returning
By keeping two objects: the original task, and the shielded task. You pass the shielded task to whatever function it is that might end up canceling it, and you await the original one. For example:
async def coro():
print('starting')
await asyncio.sleep(2)
print('done sleep')
async def cancel_it(some_task):
await asyncio.sleep(0.5)
some_task.cancel()
print('cancellation effected')
async def main():
loop = asyncio.get_event_loop()
real_task = loop.create_task(coro())
shield = asyncio.shield(real_task)
# cancel the shield in the background while we're waiting
loop.create_task(cancel_it(shield))
await real_task
assert not real_task.cancelled()
assert shield.cancelled()
asyncio.get_event_loop().run_until_complete(main())
The code waits for the task to fully complete, despite its shield getting cancelled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With