开发者

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'


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜