sách gpt4 ăn đã đi

python - 为什么我不能从多处理队列中捕获 Queue.Empty 异常?

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 20:58:47 27 4
mua khóa gpt4 giày nike

我正在 try catch 如果 multiprocessing.Queue 为空时引发的 Queue.Empty 异常。以下方法不起作用:

import multiprocessing
f = multiprocessing.Queue()
thử:
f.get(True,0.1)
except Queue.Empty:
print 'foo'

这给了我一个名称错误:NameError: name 'Queue' is not defined

用 multiprocessing.Queue.Empty 替换 Queue.Empty 也无济于事。在这种情况下,它给了我一个“AttributeError:'function'对象没有属性'Empty'”异常。

câu trả lời hay nhất

您要查找的 Trống 异常不能直接在 multiprocessing 模块中使用,因为 multiprocessing 借用了它>queue 模块(在 Python 2 中曾被命名为 Hàng đợi)。要使您的代码正常工作,只需在顶部执行 import queue:

试试这个:

import multiprocessing
import queue # or Queue in Python 2

f = multiprocessing.Queue()
thử:
f.get(True,0.1)
except queue.Empty: # queue here refers to the module, not a class
print('foo')

关于python - 为什么我不能从多处理队列中捕获 Queue.Empty 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941562/

27 4 0
Cựu chiến binh CNTT
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress