如果线程的持续时间超过10秒,如何重新启动线程?

时间:2018-05-21 11:53:36

标签: python multiprocessing pool

如何在主线程中跟踪函数write_file()的持续时间? 任务:创建条件,如果函数的执行时间超过10秒,则需要重启函数。

from multiprocessing import Pool


def write_file(file: str):
    f = open(file, 'w')
    for item in range(0, 1500000):
        f.write("%s\n" % 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)

1 个答案:

答案 0 :(得分:0)

我发现尝试修改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]