[英]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
请注意,对于这些类型的任务,最好使用进程代替线程(用ProccessPoolExecutor替换ThreadPoolExecutor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.