I need a structure where I will be able to pop()
and append()
to the right side (just like deque), while having the structure blocking and waiting if it is empty (just like Queue). I could directly use a Queue, but I need also the nice feature of deque where items are removed without blocking if the structure is full.
from collections import deque
d = deque(maxlen=2)
d.append(1)
d.append(2)
d.append(3) # d should be [2,3] (it is the case)
d.pop()
d.pop()
d.pop() # should wait (not the case)
Is it better to subclass deque (making it wait) or Queue (adding a popLeft function)?
What about creating your own queue mixing the best of both?
import queue as Queue
from collections import deque
class QueuePro:
def __init__(self, maxlenDeque):
self.deque = deque(maxlen=maxlenDeque)
self.queue = Queue.Queue()
def append(self, elem):
self.deque.append(elem)
self.queue.put(elem)
def pop(self):
if(not self.deque):
self.queue.get()
else:
self.deque.pop()
self.queue.get()
q2 = QueuePro(2)
q2.append(1)
q2.append(2)
q2.pop()
q2.pop()
q2.pop()
#waiting