无法访问 Queue.Empty:“AttributeError:“function”对象没有属性“Empty”

发布于 2024-11-17 13:58:00 字数 672 浏览 3 评论 0原文

由于某种原因,我无法访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居者 2024-11-24 13:58:01

在 Python 3 中,这是一种方法。注意小写的“队列”:

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

In Python 3, this is one way to do it. Note the lowercase 'queue':

from multiprocessing import Queue
from queue import Empty as QueueEmpty
q = Queue()
try:
    q.get(False)
except QueueEmpty:
    print "Queue was empty"
我早已燃尽 2024-11-24 13:58:00

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 )
except Queue.Empty:
    print "Queue was 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文