Python多进程处理文件
- 如果你处理的文件之间相互之间没有影响,并且产生的结果相互独立,即不会产生脏数据。可以用进程池的方法加快处理速度。
- 下面是一个例子,你们可以根据自己的需求改写代码。
import import concurrent.futures
def fun(i):
count = 0
while True:
if count > i * 99999999:
break
count += 1
def main():
# max_worker 是最大线程数。建议计算机的线程数减 1。我的笔记本是8线程,所以我设置 max_workers = 7
with concurrent.futures.ProcessPoolExecutor(max_workers=7) as executor:
parameter_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
executor.map(fun, parameter_list)
if __name__ == '__main__':
main()