在这种情况下,如何在 python 中使用线程池

How to use pool of threads in python, in this case

假设我想处理一个文件中的每一行中的字符串,它们之间相互不依赖。我平时是怎么做的:

def process_line(filelines, i):
    #process lines
import threading
threads = []
lines = open(file).readlines()
for i in range(0,len(lines)):
    threads.append(threading.Thread(target=process_line, args=(lines,i,))

[t.start() for t in threads]
[t.join() for t in threads]
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as executor:
    [executor.submit(proccess_line, lines, i) for i in range(len(lines))]

ThreadPoolExecutor 文档:https://docs.python.org/3/library/concurrent.futures.html

请注意,对于这类任务最好使用进程而不是线程(将 ThreadPoolExecutor 替换为 ProccessPoolExecutor