Ir al contenido

Fire-and-forget task (ARCH2021)

Esta página aún no está disponible en tu idioma.

Key python.async.fire-and-forget-task · Severity warning · Category async

Leaving a coroutine or task as a bare expression statement without awaiting, assigning, or returning it loses exceptions and makes ordering undefined. The task runs unsupervised in the background and its failures vanish silently.

import asyncio
async def handle():
asyncio.create_task(send_email()) # unawaited, errors are lost
return "ok"
import asyncio
async def handle():
task = asyncio.create_task(send_email())
await task
return "ok"

Await the created task, return it, or manage its lifetime and exceptions with a TaskGroup.