# 线程池:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(function, iterable)
# 进程池:
from concurrent.futures import ProcessPoolExecutor
with ProcessPoolExecutor(max_workers=5) as executor:
executor.map(function, iterable)
官方参考资料:https://pythonhosted.org/futures/
下面是一个重要实例
from concurrent.futures import ProcessPoolExecutor
from functools import partial
with ProcessPoolExecutor(max_workers=50) as executor:
# 默认参数在这里输入进去
func = partial(
conf_matrix_per_image,
num_classes=xxx,
cls_mapping=xxx)
# 注意:结果可以被顺序收集
rets = executor.map(func, preds, gt_files) # 这里的参数是可迭代参数,比如list
conf_matrix = np.zeros(
[seg_num_classes, seg_num_classes],
dtype=np.float64,
)
for ret in rets:
conf_matrix += ret
如果需要tqdm进度条
with ProcessPoolExecutor(max_workers=8) as executor:
list(tqdm(executor.map(do_process, lines), total=len(lines)))
相比于Multiprocessing,本质区别并不大,有的也只是调用方式略有差异。先有的 multiprocessing,后有的 concurrent.futures,后者的出现就是为了降低编写代码的难度,后者的学习成本较低。
既能多线程,也可以多进程
Multiprocessing 即有线程池,也是进程池,简单的使用方法如下:
# 线程池:
from multiprocessing.dummy import Pool as ThreadPool
with ThreadPool(processes=100) as executor:
executor.map(func, iterable)
# 进程池:
from multiprocessing import Pool as ProcessPool
with ProcessPool(processes=10) as executor:
executor.map(func, iterable)
下面是个实例,也可以这么写,并且经过我的尝试,这样写更快。
# 也可以直接这么写,
for idx_in_batch in range(batch_size):
p = Process(target=per_batch_processing, args=(
batch_fns,
idx_in_batch,
))
p.start()
processing.append(p)
# all over
for process in processing:
process.join()
https://blog.csdn.net/Blateyang/article/details/78088851
https://www.liaoxuefeng.com/wiki/1016959663602400/1017968846697824
对于IO密集型任务:
单进程单线程直接执行用时:10.0333秒
多线程执行用时:4.0156秒
多进程执行用时:5.0182秒
说明多线程适合IO密集型任务。
对于计算密集型任务
单进程单线程直接执行用时:10.0273秒
多线程执行用时:13.247秒
多进程执行用时:6.8377秒
https://blog.csdn.net/guyue35/article/details/92383687
for ...
for ..
write json
write txt
因为并行的时候不容易写入同一个 json (当然也可以使用apply_async,但是只能写入同一个json)
https://blog.csdn.net/qq_45383393/article/details/106154967
https://donggeitnote.com/2020/07/20/python-multiprocessing/