helpers.py 597 B

12345678910111213141516171819202122
  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
  13. def celsius_to_fahrenheit(celsius: float) -> float:
  14. """Convert temperature from Celsius to Fahrenheit."""
  15. return (celsius * 9 / 5) + 32