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)?
Not sure which is better, but here's an idea for adding wait on pop with threading.Event
from collections import deque
from threading import Event
class MyDeque(deque):
def __init__(self, max_length):
super().__init__(maxlen=max_length)
self.not_empty = Event()
self.not_empty.set()
def append(self, elem):
super().append(elem)
self.not_empty.set()
def pop(self):
self.not_empty.wait() # Wait until not empty, or next append call
if not (len(q) - 1):
self.not_empty.clear()
return super().pop()
q = MyDeque(2)
q.append(1)
q.append(2)
q.append(3)
q.pop()
q.pop()
q.pop() # Waits
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With