Python 3.x 主线程不会等待所有ThreadPoolExecutor线程完成
ThreadPoolExecutor创建的线程在for循环的第一次迭代后返回。主线程不会等待整个for循环完成。进一步检查后,我意识到,如果我用一些伪打印来替换re.sub,则循环将完全执行。在线程中使用re.sub()有什么问题Python 3.x 主线程不会等待所有ThreadPoolExecutor线程完成,python-3.x,multithreading,threadpoolexecutor,concurrent.futures,Python 3.x,Multithreading,Threadpoolexecutor,Concurrent.futures,ThreadPoolExecutor创建的线程在for循环的第一次迭代后返回。主线程不会等待整个for循环完成。进一步检查后,我意识到,如果我用一些伪打印来替换re.sub,则循环将完全执行。在线程中使用re.sub()有什么问题 import concurrent.futures import threading def process_file(file): with open(file, 'rb+') as in: mm = mmap.mmap(in.filen
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)
您的代码内部有各种错误,但已禁用,为了查看错误,您需要返回
Executor.map
的迭代器,该迭代器引用自手册:
如果func调用引发异常,则从迭代器检索其值时将引发该异常
例如:
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
results = executor.map(process_file, files)
print(list(results))