Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not catch a Queue.Empty exception from a multiprocessing Queue?

I'm trying to catch a Queue.Empty exception that is raised if a multiprocessing.Queue is empty. The following does not work:

import multiprocessing f = multiprocessing.Queue() try:       f.get(True,0.1) except Queue.Empty:       print 'foo' 

This gives me a name error: NameError: name 'Queue' is not defined

replacing Queue.Empty with multiprocessing.Queue.Empty does not help either. In this case it gives me a "AttributeError: 'function' object has no attribute 'Empty'" exception.

like image 866
Alexander Avatar asked Dec 18 '12 21:12

Alexander


People also ask

How do you know if a multiprocessing queue is empty?

We can print the contents of the multiprocessing using the get() method, empty() method and the print() function. We will check if the multiprocessing queue is empty or not using the empty() method.

Is multiprocessing queue slow?

In other words, Multiprocess queue is pretty slow putting and getting individual data, then QuickQueue wrap several data in one list, this list is one single data that is enqueue in the queue than is more quickly than put one individual data.

What is a multiprocessing queue?

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.


1 Answers

The Empty exception you're looking for isn't available directly in the multiprocessing module, because multiprocessing borrows it from the queue module (which used to be named Queue in Python 2). To make your code work, just do an import queue at the top:

Try this:

import multiprocessing import queue # or Queue in Python 2  f = multiprocessing.Queue() try:     f.get(True,0.1) except queue.Empty: # queue here refers to the module, not a class     print('foo') 
like image 149
Blckknght Avatar answered Oct 06 '22 05:10

Blckknght