|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import contextlib |
| 5 | +import math |
| 6 | +import queue |
| 7 | +import threading |
| 8 | +import time |
| 9 | +import uuid |
| 10 | +from asyncio import Task |
| 11 | +from collections.abc import AsyncIterator, Iterable, Iterator |
| 12 | +from multiprocessing import Queue, get_context |
| 13 | +from multiprocessing.process import BaseProcess |
| 14 | +from multiprocessing.synchronize import Barrier, Event |
| 15 | +from threading import Event as ThreadingEvent |
| 16 | +from typing import Any, Generic, TypeVar, Literal |
| 17 | +from multiprocessing.synchronize import Event as ProcessingEvent |
| 18 | + |
| 19 | +import culsans |
| 20 | + |
| 21 | +from guidellm.config import settings |
| 22 | +from guidellm.scheduler.constraints import Constraint |
| 23 | +from guidellm.scheduler.objects import ( |
| 24 | + BackendInterface, |
| 25 | + MeasuredRequestTimingsT, |
| 26 | + MultiTurnRequestT, |
| 27 | + RequestT, |
| 28 | + ResponseT, |
| 29 | + ScheduledRequestInfo, |
| 30 | + SchedulerState, |
| 31 | +) |
| 32 | +from guidellm.scheduler.strategy import SchedulingStrategy |
| 33 | +from guidellm.scheduler.worker import WorkerProcess |
| 34 | +from guidellm.utils import MsgpackEncoding, synchronous_to_exitable_async |
| 35 | + |
| 36 | + |
| 37 | +__all__ = [ |
| 38 | + "WorkerQueueProxy", |
| 39 | +] |
| 40 | + |
| 41 | + |
| 42 | +MessageT = TypeVar("MessageT", bound=Any) |
| 43 | + |
| 44 | + |
| 45 | +class WorkerQueueProxy(Generic[MessageT]): |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + mp_queue: Queue[MessageT], |
| 49 | + usage: Literal["producer", "consumer"], |
| 50 | + stopped_event: ThreadingEvent | ProcessingEvent | None = None, |
| 51 | + stop_events: list[ThreadingEvent | ProcessingEvent | None] = None, |
| 52 | + on_stop_event: Literal["continue", "stop", "error"] = "stop", |
| 53 | + on_queue_empty: Literal["continue", "stop", "stop_if_event", "error"] = "stop", |
| 54 | + on_queue_full: Literal["continue", "stop", "stop_if_event", "error"] = "stop", |
| 55 | + on_queue_shutdown: Literal[ |
| 56 | + "continue", "stop", "stop_if_event", "error" |
| 57 | + ] = "stop", |
| 58 | + poll_interval: float = 0.1, |
| 59 | + ): |
| 60 | + self.mp_queue = mp_queue |
| 61 | + self.usage = usage |
| 62 | + self.stopped_event = stopped_event |
| 63 | + self.stop_events = stop_events |
| 64 | + self.on_stop_event = on_stop_event |
| 65 | + self.on_queue_empty = on_queue_empty |
| 66 | + self.on_queue_full = on_queue_full |
| 67 | + self.on_queue_shutdown = on_queue_shutdown |
| 68 | + self.poll_interval = poll_interval |
| 69 | + |
| 70 | + self.local_queue: culsans.Queue[MessageT] = culsans.Queue() |
| 71 | + self.running = False |
| 72 | + |
| 73 | + async def run(self): |
| 74 | + self.running = True |
| 75 | + func = ( |
| 76 | + self._producer_generator |
| 77 | + if self.usage == "producer" |
| 78 | + else self._consumer_generator |
| 79 | + ) |
| 80 | + await synchronous_to_exitable_async(synchronous=func(), poll_interval=0.0) |
| 81 | + self.running = False |
| 82 | + |
| 83 | + def sync_put( |
| 84 | + self, item: MessageT, block: bool = True, timeout: float | None = None |
| 85 | + ): |
| 86 | + if self.usage != "producer": |
| 87 | + raise ValueError("WorkerQueueProxy is not a producer") |
| 88 | + |
| 89 | + self.local_queue.sync_put(item, block=block, timeout=timeout) |
| 90 | + |
| 91 | + def sync_put_nowait(self, item: MessageT): |
| 92 | + if self.usage != "producer": |
| 93 | + raise ValueError("WorkerQueueProxy is not a producer") |
| 94 | + |
| 95 | + self.local_queue.put_nowait(item) |
| 96 | + |
| 97 | + async def async_put(self, item: MessageT, timeout: float | None = None): |
| 98 | + if self.usage != "producer": |
| 99 | + raise ValueError("WorkerQueueProxy is not a producer") |
| 100 | + |
| 101 | + await asyncio.wait_for(self.local_queue.async_put(item), timeout) |
| 102 | + |
| 103 | + def sync_get(self, block: bool = True, timeout: float | None = None) -> MessageT: |
| 104 | + if self.usage != "consumer": |
| 105 | + raise ValueError("WorkerQueueProxy is not a consumer") |
| 106 | + |
| 107 | + return self.local_queue.sync_get(block=block, timeout=timeout) |
| 108 | + |
| 109 | + def sync_get_nowait(self) -> MessageT: |
| 110 | + if self.usage != "consumer": |
| 111 | + raise ValueError("WorkerQueueProxy is not a consumer") |
| 112 | + |
| 113 | + return self.local_queue.get_nowait() |
| 114 | + |
| 115 | + async def async_get(self, timeout: float | None = None) -> MessageT: |
| 116 | + if self.usage != "consumer": |
| 117 | + raise ValueError("WorkerQueueProxy is not a consumer") |
| 118 | + |
| 119 | + return await asyncio.wait_for(self.local_queue.async_get(), timeout) |
| 120 | + |
| 121 | + def _producer_generator(self): |
| 122 | + last_yield_time = time.time() |
| 123 | + |
| 124 | + while True: |
| 125 | + stop_set = ( |
| 126 | + any(event.is_set() for event in self.stop_events) |
| 127 | + if self.stop_events |
| 128 | + else False |
| 129 | + ) |
| 130 | + |
| 131 | + if stop_set and self.on_stop_event == "stop": |
| 132 | + break |
| 133 | + |
| 134 | + if stop_set and self.on_stop_event == "error": |
| 135 | + raise RuntimeError( |
| 136 | + "WorkerQueueProxy stop event set unexpectedly " |
| 137 | + "(on_stop_event==error)" |
| 138 | + ) |
| 139 | + |
| 140 | + if self.on_stop_event != "continue" and any( |
| 141 | + event.is_set() for event in self.stop_events |
| 142 | + ): |
| 143 | + if self.on_stop_event == "stop": |
| 144 | + break |
| 145 | + if self.on_stop_event == "error": |
| 146 | + raise RuntimeError( |
| 147 | + "WorkerQueueProxy stop event set unexpectedly " |
| 148 | + "(on_stop_event==error)" |
| 149 | + ) |
| 150 | + |
| 151 | + def _consumer_generator(self): |
| 152 | + pass |
0 commit comments