Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Queue import Queue, Full
- class LimitQueue(Queue):
- def put(self, item):
- while True:
- try:
- Queue.put(self, item, block=False)
- except Full:
- try:
- self.queue.popleft()
- except Empty:
- pass
- else:
- break
- # If you want it in reverse order replace _put and _get with these:
- def _put(self, item):
- self.queue.appendleft(item)
- def _get(self):
- return self.queue.pop()
- if __name__ == "__main__":
- q = LimitQueue(maxsize=3)
- q.put(1)
- q.put(2)
- q.put(3)
- q.put(4)
- print(q.queue)
- # Output: deque([2, 3, 4])
Add Comment
Please, Sign In to add comment