2024-10-29 06:13:26 +00:00
|
|
|
#encoding = utf8
|
|
|
|
|
|
|
|
import threading
|
|
|
|
from queue import Queue
|
|
|
|
|
2024-10-31 13:38:35 +00:00
|
|
|
|
2024-10-29 06:13:26 +00:00
|
|
|
class SyncQueue:
|
2024-11-01 12:38:57 +00:00
|
|
|
def __init__(self, maxsize, name):
|
|
|
|
self._name = name
|
2024-10-29 06:13:26 +00:00
|
|
|
self._queue = Queue(maxsize)
|
|
|
|
self._condition = threading.Condition()
|
|
|
|
|
2024-11-07 00:26:03 +00:00
|
|
|
def is_empty(self):
|
|
|
|
return self._queue.empty()
|
|
|
|
|
2024-10-29 06:13:26 +00:00
|
|
|
def put(self, item):
|
|
|
|
with self._condition:
|
|
|
|
while self._queue.full():
|
2024-11-01 12:38:57 +00:00
|
|
|
# print(self._name, 'put wait')
|
2024-10-29 06:13:26 +00:00
|
|
|
self._condition.wait()
|
|
|
|
self._queue.put(item)
|
|
|
|
self._condition.notify()
|
|
|
|
|
2024-11-01 12:38:57 +00:00
|
|
|
def get(self, timeout=None):
|
|
|
|
# 添加超时时间,防止死锁
|
2024-10-29 06:13:26 +00:00
|
|
|
with self._condition:
|
|
|
|
while self._queue.empty():
|
2024-11-01 12:38:57 +00:00
|
|
|
self._condition.wait(timeout=timeout)
|
|
|
|
# print(self._name, 'get wait')
|
|
|
|
if timeout is not None:
|
|
|
|
break
|
|
|
|
item = self._queue.get(block=False)
|
2024-10-29 06:13:26 +00:00
|
|
|
self._condition.notify()
|
|
|
|
return item
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
with self._condition:
|
|
|
|
while not self._queue.empty():
|
2024-10-30 08:34:12 +00:00
|
|
|
self._queue.queue.clear()
|
2024-11-01 12:38:57 +00:00
|
|
|
self._condition.notify()
|
2024-10-29 10:09:26 +00:00
|
|
|
|
|
|
|
def size(self):
|
|
|
|
return self._queue.qsize()
|
2024-10-30 08:34:12 +00:00
|
|
|
|
|
|
|
|
2024-10-31 13:38:35 +00:00
|
|
|
'''
|
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()
|
2024-10-31 13:38:35 +00:00
|
|
|
'''
|