CKTIS / Python
asyncio.shield
Ščiti opravilo pred preklicem, prenesenim prek ovoja.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
await asyncio.shield(aw)Ščiti opravilo pred preklicem, prenesenim prek ovoja.
Klicatelj še vedno prejme CancelledError in prvotno opravilo se nadaljuje. Neposreden preklic opravila doseže tudi ovoj. Ohranite močan sklic na opravilo.
V primeru je outer preklican, toda signal da True brez preklica task.
import asyncio
async def main():
e = asyncio.Event()
t = asyncio.create_task(e.wait())
s = asyncio.shield(t)
s.cancel()
try:
await s
except asyncio.CancelledError:
assert not t.cancelled()
e.set()
assert await t is True
asyncio.run(main())
Primeri
import asyncio
async def main():
gate = asyncio.Event()
task = asyncio.create_task(gate.wait())
outer = asyncio.shield(task)
outer.cancel()
gate.set()
print(outer.cancelled(), await task, task.cancelled())
asyncio.run(main())
Rezultat
True True False
V primeru je outer preklican, toda signal da True brez preklica task.