Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a multiprocessing queue in python

I just want to know how to clear a multiprocessing queue in python like a normal python queue. For instance:

from multiprocessing import Queue  # multiprocessing queue from Queue import Queue            # normal queue  multi_q = Queue() normal_q = Queue() multi_q.clear()                    # or multi_q.queue.clear()  

'Queue' object has no attribute 'clear'

normal_q.queue.clear() # This is ok 
like image 468
FelipeG Avatar asked May 09 '13 12:05

FelipeG


People also ask

How do I clear my multiprocessing queue?

Simply use q = ClearableQueue() in all places where you used q = Queue() , and call q. clear() when you'd like.

How do you close a multiprocessing process in Python?

We can kill or terminate a process immediately by using the terminate() method. We will use this method to terminate the child process, which has been created with the help of function, immediately before completing its execution.

What is queue in Python multiprocessing?

Queue class. A queue is a data structure on which items can be added by a call to put() and from which items can be retrieved by a call to get(). The multiprocessing. Queue provides a first-in, first-out FIFO queue, which means that the items are retrieved from the queue in the order they were added.


2 Answers

So, I take look at Queue class, and you may to try this code:

while not some_queue.empty():     some_queue.get()  # as docs say: Remove and return an item from the queue. 
like image 110
Pavel Stárek Avatar answered Sep 28 '22 07:09

Pavel Stárek


Ask for forgiveness rather than permission; just try to empty the queue until you get the Empty exception, then ignore that exception:

from Queue import Empty  def clear(q):     try:         while True:             q.get_nowait()     except Empty:         pass 

Better yet: is a built-in class missing the method you want? Subclass the built-in class, and add the method you think should be there!

from Queue import Queue, Empty  class ClearableQueue(Queue):      def clear(self):         try:             while True:                 self.get_nowait()         except Empty:             pass 

Your ClearableQueue class inherits all the goodness (and behavior) of the built-in Queue class, and has the method you now want.

Simply use q = ClearableQueue() in all places where you used q = Queue(), and call q.clear() when you'd like.

like image 41
Dan H Avatar answered Sep 28 '22 06:09

Dan H