Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access Queue.Empty: "AttributeError: 'function' object has no attribute 'Empty'"

For some reason I can't access the Queue.Empty exception - what am I doing wrong here?

from multiprocessing import Process, Queue

# ...

try:
    action = action_queue.get(False)
    print "Action: " + action
except Queue.Empty:
    pass

The stack trace:

Traceback (most recent call last):  
File "C:\Program Files\Python27\lib\multiprocessing\process.py", line 258,
  in _bootstrap
  self.run()
File "C:\Program Files\Python27\lib\multiprocessing\process.py", line 114,
  in run
  self._target(*self._args, **self._kwargs)
File "D:\Development\populate.py", line 39, in permutate
  except Queue.Empty: AttributeError: 'function' object has no attribute 'Empty'
like image 954
Ross Avatar asked Jun 27 '11 11:06

Ross


1 Answers

The Queue.Empty exception is in the Queue module, not in the multiprocessing.queues.Queue class. The multiprocessing module actually uses the Queue (module) Empty exception class:

from multiprocessing import Queue
from Queue import Empty
q = Queue()
try:
    q.get( False )
except Empty:
    print "Queue was empty"

If you want to be very explicit and verbose, you can do this:

import multiprocessing
import Queue
q = multiprocessing.Queue()
try:
    q.get( False )
except Queue.Empty:
    print "Queue was empty"

Favoring the former approach is probably a better idea because there is only one Queue object to worry about and you don't have to wonder if you are working with the class or the module as in my second example.

like image 150
underrun Avatar answered Nov 13 '22 06:11

underrun