出于某种原因,我无法访问Queue.Empty
例外 - 我在这里做错了什么?
from multiprocessing import Process, Queue
# ...
try:
action = action_queue.get(False)
print "Action: " + action
except Queue.Empty:
pass
堆栈跟踪:
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'
答案 0 :(得分:26)
Queue.Empty异常位于Queue模块中,而不是multiprocessing.queues.Queue类中。多处理模块实际使用Queue(模块)Empty异常类:
from multiprocessing import Queue
from Queue import Empty
q = Queue()
try:
q.get( False )
except Empty:
print "Queue was empty"
如果你想要非常明确和冗长,你可以这样做:
import multiprocessing
import Queue
q = multiprocessing.Queue()
try:
q.get( False )
execpt Queue.Empty:
print "Queue was empty"
支持前一种方法可能是一个更好的主意,因为只有一个Queue对象需要担心,你不必怀疑你是否正在使用类或模块,如我的第二个例子。