On this page
  1. asyncio.shield
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

asyncio.shield

Prev: asyncio.set_event_loop_policy | Up: asyncio | Next: asyncio.sleep
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.shield
Tags: #public #python3 #stdlib #asyncio


Import

import asyncio
from asyncio import shield

Signature & Overview

asyncio.shield(arg)

Wait for a future, shielding it from cancellation.

Usage

import asyncio

async def critical() -> str:
    await asyncio.sleep(0.01)
    return "saved"

async def main():
    inner = asyncio.create_task(critical())
    shielded = asyncio.shield(inner)
    shielded.cancel()
    assert await inner == "saved"

asyncio.run(main())

Gotchas

REPL Inspect

from asyncio import shield
help(shield)

Linked from

Related notes