File size: 725 Bytes
e791b16 | 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 | """Prompt queue actor for orchestration.
The queue wraps an asyncio.Queue. It provides
`add_prompt` (async) and `get_prompt` methods that can be called
from Ray actors or standalone threads.
"""
import asyncio
try:
import ray
ray_remote = ray.remote
except ImportError:
ray = None
def ray_remote(cls):
return cls
@ray_remote
class PromptQueue:
def __init__(self, maxsize: int = 0):
self._queue = asyncio.Queue(maxsize=maxsize)
async def add_prompt(self, prompt: dict):
await self._queue.put(prompt)
return True
async def get_prompt(self):
prompt = await self._queue.get()
return prompt
def size(self):
return self._queue.qsize()
|