human/utils/sync_queue.py

33 lines
836 B
Python
Raw Normal View History

2024-10-29 06:13:26 +00:00
#encoding = utf8
import threading
from queue import Queue
class SyncQueue:
def __init__(self, maxsize):
self._queue = Queue(maxsize)
self._condition = threading.Condition()
def put(self, item):
with self._condition:
while self._queue.full():
self._condition.wait()
self._queue.put(item)
self._condition.notify()
def get(self):
with self._condition:
while self._queue.empty():
self._condition.wait()
item = self._queue.get()
self._condition.notify()
return item
def clear(self):
with self._condition:
while not self._queue.empty():
self._queue.get()
self._queue.task_done()
self._condition.notify_all()