如何在主线程中跟踪函数 write_file(( 的持续时间? 任务:创建一个条件,如果函数的执行时间超过 10 秒,则需要重启函数。
from multiprocessing import Pool
def write_file(file: str):
f = open(file, 'w')
for item in range(0, 1500000):
f.write("%sn" % item)
f.close()
if __name__ == '__main__':
list_files = ['1.txt', '2.txt', '3.txt']
with Pool(3) as p:
p.map(write_file, list_files)
我发现在这里修改 Pool 的尝试过于复杂。 Pool 类允许工作线程在整个工作队列完成之前处于活动状态,因此具有复杂的机制来控制它。
相反,如果你对 10 秒的要求不是很严格,你可以使用以下代码:
from multiprocessing import Process
import time
pdict = {}
for fname in list_files:
p = Process(target = write_file, args = (fname,))
pdict[fname] = p
p.start()
while pdict:
to_del = []
time.sleep(10)
for pname in pdict:
if pdict[pname].exitcode == None or pdict[pname].is_alive():
pdict[pname].terminate() #killing old; that should also release file resource
pdict[pname] = Process(target = write_file, args = (pname,))
pdict[pname].start() #simply creating new and starting
else:
to_del.append(pname)
for pname in to_del:
del pdict[pname]