Guest User

Untitled

a guest
Jul 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. from Queue import Queue, Full
  2.  
  3. class LimitQueue(Queue):
  4. def put(self, item):
  5. while True:
  6. try:
  7. Queue.put(self, item, block=False)
  8. except Full:
  9. try:
  10. self.queue.popleft()
  11. except Empty:
  12. pass
  13. else:
  14. break
  15.  
  16.  
  17. # If you want it in reverse order replace _put and _get with these:
  18. def _put(self, item):
  19. self.queue.appendleft(item)
  20. def _get(self):
  21. return self.queue.pop()
  22.  
  23.  
  24. if __name__ == "__main__":
  25. q = LimitQueue(maxsize=3)
  26.  
  27. q.put(1)
  28. q.put(2)
  29. q.put(3)
  30. q.put(4)
  31. print(q.queue)
  32. # Output: deque([2, 3, 4])
Add Comment
Please, Sign In to add comment