1
0

helpers.py 451 B

1234567891011121314151617
  1. from __future__ import annotations
  2. import asyncio
  3. from collections.abc import Coroutine
  4. from typing import Any, TypeVar
  5. _R = TypeVar("_R")
  6. _BACKGROUND_TASKS: set[asyncio.Task[Any]] = set()
  7. def create_background_task(target: Coroutine[Any, Any, _R]) -> asyncio.Task[_R]:
  8. """Create a background task."""
  9. task = asyncio.create_task(target)
  10. _BACKGROUND_TASKS.add(task)
  11. task.add_done_callback(_BACKGROUND_TASKS.remove)
  12. return task