Python 多处理获取不会超时
Python Multiprocessing get does not timeout
我正在测试一些代码以使用 Process
和 Queue
的多处理使函数调用超时。 Queue.get()
方法采用可选的超时参数。我编写了以下测试以确认当被调用进程花费的时间比调用 get
时分配的时间长时它会抛出超时错误,但它不会抛出错误。谁能告诉我我是如何未能正确测试 get
超时的?我在 Windows 7 和 python 2.
import time
from multiprocessing import Process, Queue
def f(q, t):
time.sleep(t)
q.put(0)
if __name__ == '__main__':
q = Queue()
p = Process(target=f, args=(q, 15, ))
p.start()
x = q.get(1)
print "received ", x
从 documentation 中,Queue.get
收到 2 个参数:block
和 timeout
,顺序为。你应该这样称呼它
q.get(timeout=1)
我正在测试一些代码以使用 Process
和 Queue
的多处理使函数调用超时。 Queue.get()
方法采用可选的超时参数。我编写了以下测试以确认当被调用进程花费的时间比调用 get
时分配的时间长时它会抛出超时错误,但它不会抛出错误。谁能告诉我我是如何未能正确测试 get
超时的?我在 Windows 7 和 python 2.
import time
from multiprocessing import Process, Queue
def f(q, t):
time.sleep(t)
q.put(0)
if __name__ == '__main__':
q = Queue()
p = Process(target=f, args=(q, 15, ))
p.start()
x = q.get(1)
print "received ", x
从 documentation 中,Queue.get
收到 2 个参数:block
和 timeout
,顺序为。你应该这样称呼它
q.get(timeout=1)