Spaces:
Running on Zero
Running on Zero
File size: 4,011 Bytes
36333c5 eb808a5 36333c5 eb808a5 36333c5 eb808a5 36333c5 eb808a5 36333c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | """A bounded, single-worker asynchronous inference queue."""
from __future__ import annotations
import asyncio
from contextvars import Context, copy_context
from dataclasses import dataclass
from typing import Any, Callable, Generic, TypeVar
from core.errors import GatewayError, QueueCapacityError
T = TypeVar("T")
@dataclass(slots=True)
class QueueJob(Generic[T]):
request_id: str
label: str
operation: Callable[[], T]
context: Context
future: asyncio.Future[T]
started: asyncio.Future[None]
cancelled: bool = False
class InferenceQueue:
"""Serialize all accelerator work through one worker."""
def __init__(self, capacity: int, timeout_seconds: int) -> None:
self.capacity = capacity
self.timeout_seconds = timeout_seconds
self._queue: asyncio.Queue[QueueJob[Any] | None] = asyncio.Queue(maxsize=capacity)
self._worker: asyncio.Task[None] | None = None
self._active = False
@property
def depth(self) -> int:
return self._queue.qsize()
@property
def active(self) -> bool:
return self._active
async def start(self) -> None:
if self._worker is None or self._worker.done():
self._worker = asyncio.create_task(self._work(), name="inference-worker")
async def stop(self) -> None:
if self._worker is None:
return
await self._queue.put(None)
await self._worker
self._worker = None
async def submit(self, request_id: str, label: str, operation: Callable[[], T]) -> T:
"""Add a synchronous operation without blocking the event loop."""
await self.start()
loop = asyncio.get_running_loop()
future: asyncio.Future[T] = loop.create_future()
started: asyncio.Future[None] = loop.create_future()
job = QueueJob(
request_id=request_id,
label=label,
operation=operation,
context=copy_context(),
future=future,
started=started,
)
try:
self._queue.put_nowait(job)
except asyncio.QueueFull as exc:
raise QueueCapacityError() from exc
try:
await asyncio.wait_for(asyncio.shield(started), timeout=self.timeout_seconds)
except asyncio.CancelledError:
if started.done():
future.add_done_callback(self._consume_late_result)
else:
job.cancelled = True
future.cancel()
raise
except TimeoutError as exc:
if started.done():
return await asyncio.shield(future)
job.cancelled = True
future.cancel()
raise GatewayError(
"Inference timed out waiting in the queue",
status_code=504,
code="queue_timeout",
) from exc
return await asyncio.shield(future)
@staticmethod
def _consume_late_result(future: asyncio.Future[Any]) -> None:
if not future.cancelled():
try:
future.exception()
except asyncio.CancelledError:
pass
async def _work(self) -> None:
while True:
job = await self._queue.get()
if job is None:
self._queue.task_done()
break
if job.cancelled:
self._queue.task_done()
continue
self._active = True
if not job.started.done():
job.started.set_result(None)
try:
result = await asyncio.to_thread(job.context.run, job.operation)
except Exception as exc:
if not job.future.done():
job.future.set_exception(exc)
else:
if not job.future.done():
job.future.set_result(result)
finally:
self._active = False
self._queue.task_done()
|