我正在尝试在 Pebble 中设置工作超时(基本上有效)
from pebble import ProcessPool
from multiprocessing import Process, Pool, cpu_count
import time
def test_fn(randomNumberFromList):
# print(f'Beginnging for this number: {randomNumberFromList}')
if randomNumberFromList in [1,66,3,88,5,777]:
time.sleep(10)
completion_state = [randomNumberFromList, True]
return completion_state
else:
time.sleep(1)
completion_state = [randomNumberFromList, True]
return completion_state
def main():
list1 = [1, 2, 3, 4, 5, 6, 20, 33, 55, 233, 44, 66, 2666, 555, 8888, 888, 88, 77, 777, 7777, 77777]
with ProcessPool() as pool:
future = pool.map(test_fn, list1, timeout=4)
iterator = future.result()
while True:
try:
result = next(iterator)
print(result)
except StopIteration:
break
except TimeoutError as error:
print(f'list1value took longer than {error.args[1]} seconds.')
if __name__ == "__main__":
main()
但是,我无法在 TimeoutError 块内获取 list1 中的“失败”值。我该怎么做呢?对于这个例子,我想要“1花了超过4秒。”,“66花了超过4秒。”等等打印到控制台
翻遍了Pebble文档,没有看到这样的方法会给我带来价值
为了在发生
TimeoutError
时获取导致超时的
list1
中的值,你需要跟踪
ProcessPool
中每个 futures 的输入值。可以通过创建一个字典来实现,该字典将 futures 与其相应的输入值关联起来。
以下是更新后的代码:
from pebble import ProcessPool
from concurrent.futures import TimeoutError
import time
def test_fn(randomNumberFromList):
# print(f'Beginnging for this number: {randomNumberFromList}')
if randomNumberFromList in [1, 66, 3, 88, 5, 777]:
time.sleep(10)
completion_state = [randomNumberFromList, True]
return completion_state
else:
time.sleep(1)
completion_state = [randomNumberFromList, True]
return completion_state
def main():
list1 = [1, 2, 3, 4, 5, 6, 20, 33, 55, 233, 44, 66, 2666, 555, 8888, 888, 88, 77, 777, 7777, 77777]
with ProcessPool() as pool:
future_dict = {pool.schedule(test_fn, args=(num,)): num for num in list1}
for future in future_dict:
try:
result = future.result(timeout=4)
print(result)
except TimeoutError:
print(f'list1 value {future_dict[future]} took longer than 4 seconds.')
if __name__ == "__main__":
main()
在这个更新后的代码中:
-
我们创建了一个字典
future_dict
来存储 futures 和它们对应的输入值。 -
我们迭代
future_dict
中的 futures,并使用future.result(timeout=4)
来获取每个 future 的结果,并设置 4 秒的超时时间。 -
如果一个 future 超时,我们会在
TimeoutError
块中使用future_dict[future]
来访问并打印导致超时的list1
中的值。
这个修改将确保你在发生
TimeoutError
时可以识别出
list1
中导致超时的特定值。