主线程不等待所有ThreadPoolExecutor线程完成

时间:2019-08-31 16:01:31

标签: python-3.x multithreading threadpoolexecutor concurrent.futures

由ThreadPoolExecutor创建的线程在for循环中第一次迭代后返回。主线程不等到整个for循环完成。进一步检查,我意识到是否只用一些虚拟打印替换re.sub即可完全执行stdout循环。在线程中使用re.sub()有什么问题?

import concurrent.futures
import threading


def process_file(file):
    with open(file, 'rb+') as in:
        mm = mmap.mmap(in.fileno(),0)
        for i in range(len(global_list)):
            mm = re.sub(global_list[i], global_ch_list[i],mm)

    with open(file, 'wb+') as out:
        out.write(mm)


def process_all_files(files):
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        executor.map(process_file, files)


process_all_files(files)

1 个答案:

答案 0 :(得分:2)

您的代码内部有各种错误但被忽略了,要查看该错误,您需要使用Executor.map的返回迭代器,该迭代器由手册引用:

  

如果func调用引发异常,则从迭代器中检索其异常时,将引发该异常。

例如:

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
    results = executor.map(process_file, files)
    print(list(results))