【发布时间】:2021-09-27 13:59:52
【问题描述】:
我有一个多处理设置,通过将所有计算值附加到lst 来处理长时间运行的任务。大致是这样的:
from multiprocessing import Pool
from time import sleep
def fun(_):
lst = [] # list that will be returned
for i in range(200):
lst.append(i)
if not i % 10:
sleep(0.1) # 'long task', cause a KeyboardInterrupt in this time
return lst
if __name__ == '__main__':
master = []
processes = 2
for result in Pool(processes).imap_unordered(fun, range(processes)):
master.append(result)
print(master)
我希望能够引起KeyboardInterrupt 并让进程返回他们处理的列表,即使它们尚未完成,因为每次迭代只是添加一个新的子列表。
(我的实际数据大致类似于lst = ([], [[], ...], [[], ...]),每个空列表只包含整数,实际函数为return lst1, lst2, lst3)
我试图将整个主要部分包含在try: except: 中,如下所示:
try:
for result in Pool(processes).imap_unordered(fun, range(processes)):
master.append(result)
except KeyboardInterrupt:
# somehow retrieve the values here
pass
但是,我还没有以这种方式找到任何可能的解决方案。 我如何告诉进程是时候提前退出并返回他们当前的结果了?
编辑以显示实际结构: main.py:
from other import Other
class Something:
def __init__(self):
pass # stuff here
def spawner(self):
for result in Pool(processes=self.processes).imap_unordered(self.loop, range(self.processes)):
pass # do stuff with the data
def loop(self, _):
# setup stuff
Other(setup_stuff).start()
其他.py
class Other:
def __init__(self):
pass # more stuff
def start(self):
lst1, lst2, lst3 = [], [], []
for _ in range(self.episodes):
pass # do the actual computation
return lst1, lst2, lst3
【问题讨论】:
标签: python exception multiprocessing