human/utils/sync_queue.py

58 lines
1.4 KiB
Python
Raw Normal View History

2024-10-29 06:13:26 +00:00
#encoding = utf8
import threading
from queue import Queue
2024-10-30 08:34:12 +00:00
'''
2024-10-29 06:13:26 +00:00
class SyncQueue:
def __init__(self, maxsize):
self._queue = Queue(maxsize)
2024-10-30 08:34:12 +00:00
# self._queue = Queue()
2024-10-29 06:13:26 +00:00
self._condition = threading.Condition()
def put(self, item):
2024-10-30 08:34:12 +00:00
# self._queue.put(item)
2024-10-29 06:13:26 +00:00
with self._condition:
while self._queue.full():
2024-10-30 08:34:12 +00:00
print('put wait')
2024-10-29 06:13:26 +00:00
self._condition.wait()
self._queue.put(item)
self._condition.notify()
def get(self):
2024-10-30 08:34:12 +00:00
# return self._queue.get(block=True, timeout=0.01)
2024-10-29 06:13:26 +00:00
with self._condition:
while self._queue.empty():
self._condition.wait()
item = self._queue.get()
self._condition.notify()
return item
def clear(self):
2024-10-30 08:34:12 +00:00
# self._queue.queue.clear()
2024-10-29 06:13:26 +00:00
with self._condition:
while not self._queue.empty():
2024-10-30 08:34:12 +00:00
self._queue.queue.clear()
2024-10-29 06:13:26 +00:00
self._condition.notify_all()
2024-10-29 10:09:26 +00:00
def size(self):
return self._queue.qsize()
2024-10-30 08:34:12 +00:00
'''
class SyncQueue:
def __init__(self, maxsize):
self._queue = Queue()
def put(self, item):
self._queue.put(item)
def get(self):
return self._queue.get(block=True, timeout=0.2)
def clear(self):
self._queue.queue.clear()
def size(self):
return self._queue.qsize()