Python多进程并发[glob,concurrent和Pool]

简单的代码只能单核处理,当处理超大量数据时,会非常慢。

可以用concurrent 或Pool。

with concurrent.futures.ProcessPoolExecutor() as executor:
        roots = glob('/root/code/ocr/dataset/images/*.jpg')
   
         executor.map(verify.get_text, roots)

在这里插入图片描述

或者用Pool

from glob import glob
 import os
import concurrent.futures
from multiprocessing import Pool

def get_text(file_name):
	print(file_name)

roots = glob('images/*.jpg')
 79     start = time.time()
 80     pool = Pool(20) # 核数
 81     pool.map(get_text, roots) # 接口
 82     pool.close()
 83     pool.join()
 84     end = time.time()
 85     print(end - start)

这里我们看到进程池pool的map,有一个processes参数,这个参数可以不设置,如果不设置函数会跟根据计算机的实际情况来决定要运行多少个进程,我们也可自己设置,但是要考虑自己计算机的性能

https://blog.csdn.net/weixin_36637463/article/details/86496763

https://blog.csdn.net/tMb8Z9Vdm66wH68VX1/article/details/82955293

也可以用pands 分批加载处理

https://blog.csdn.net/htbeker/article/details/86542412

猜你喜欢

转载自blog.csdn.net/wwlhz/article/details/103506052