23 lines
532 B
Python
23 lines
532 B
Python
|
|
import asyncio
|
||
|
|
import logging
|
||
|
|
from typing import Awaitable, Callable
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
_queue: asyncio.Queue[Callable[[], Awaitable[None]]] = asyncio.Queue()
|
||
|
|
|
||
|
|
|
||
|
|
async def enqueue(task: Callable[[], Awaitable[None]]) -> None:
|
||
|
|
await _queue.put(task)
|
||
|
|
|
||
|
|
|
||
|
|
async def worker_loop() -> None:
|
||
|
|
while True:
|
||
|
|
task = await _queue.get()
|
||
|
|
try:
|
||
|
|
await task()
|
||
|
|
except Exception:
|
||
|
|
logger.exception("Unhandled error in worker task")
|
||
|
|
finally:
|
||
|
|
_queue.task_done()
|