33 lines
836 B
Python
33 lines
836 B
Python
|
#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()
|