无法访问 Queue.Empty:“AttributeError:“function”对象没有属性“Empty”
由于某种原因,我无法访问 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'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Python 3 中,这是一种方法。注意小写的“队列”:
In Python 3, this is one way to do it. Note the lowercase 'queue':
Queue.Empty 异常位于 Queue 模块中,而不是位于 multiprocessing.queues.Queue 类中。多处理模块实际上使用 Queue(模块)Empty 异常类:
如果您想要非常明确和详细,您可以这样做:
支持前一种方法可能是一个更好的主意,因为只有一个 Queue 对象需要担心,并且您 可以这样做:不必像我的第二个示例中那样怀疑您是否正在使用类或模块。
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:
If you want to be very explicit and verbose, you can do this:
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.