forked from feiskyer/koder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
59 lines (48 loc) · 1.81 KB
/
Copy pathqueue.py
File metadata and controls
59 lines (48 loc) · 1.81 KB
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
"""Async message queue for inter-component communication."""
import asyncio
from collections import deque
from typing import Any, Deque, Optional
class AsyncMessageQueue:
"""Async message queue for managing communication between components."""
def __init__(self, maxsize: int = 100):
self.queue: Deque[Any] = deque(maxlen=maxsize)
self.event = asyncio.Event()
self.lock = asyncio.Lock()
async def put(self, item: Any) -> None:
"""Put an item in the queue."""
async with self.lock:
self.queue.append(item)
self.event.set()
async def get(self) -> Any:
"""Get an item from the queue, waiting if necessary."""
while True:
async with self.lock:
if self.queue:
item = self.queue.popleft()
if not self.queue:
self.event.clear()
return item
await self.event.wait()
async def get_nowait(self) -> Optional[Any]:
"""Get an item from the queue without waiting."""
async with self.lock:
if self.queue:
item = self.queue.popleft()
if not self.queue:
self.event.clear()
return item
return None
def qsize(self) -> int:
"""Return the approximate size of the queue."""
return len(self.queue)
def empty(self) -> bool:
"""Return True if the queue is empty."""
return len(self.queue) == 0
def full(self) -> bool:
"""Return True if the queue is full."""
return len(self.queue) == self.queue.maxlen
async def clear(self) -> None:
"""Clear all items from the queue."""
async with self.lock:
self.queue.clear()
self.event.clear()