python multiprocessing.Pool 中map、map_async、apply、apply_async的区别,, multipro
python multiprocessing.Pool 中map、map_async、apply、apply_async的区别,, multipro
multiprocessing是python的多进程库,multiprocessing.dummy则是多线程的版本,使用都一样。
其中都有pool池的概念,进程池/线程池有共同的方法,其中方法对比如下 :
There are four choices to mapping jobs to process. Here are the differences:
Multi-args Concurrence Blocking Ordered-resultsmap no yes yes yesapply yes no yes nomap_async no yes no yesapply_async yes yes no no
In Python 3, a new functionstarmapcan accept multiple arguments.
Note thatmapandmap_asyncarecalled for a list of jobs in one time, butapplyandapply_async can only called for one job. However,apply_asyncexecute a job in background thereforein parallel.See examples:
# mapresults = pool.map(worker, [1, 2, 3])# applyfor x, y in [[1, 1], [2, 2]]: results.append(pool.apply(worker, (x, y)))def collect_result(result): results.append(result)# map_asyncpool.map_async(worker, jobs, callback=collect_result)# apply_asyncfor x, y in [[1, 1], [2, 2]]: pool.apply_async(worker, (x, y), callback=collect_result)
相关内容
- 从错误中学python(2)————字符串转浮点数,, 题
- arcpy arcgis python实例教程--原点夹角距离定义线(坐标正
- 抖音美女千千万,想用Python爬爬看,,前言文的文字及图
- python 快速排序 完整,,两头开始 以第一个为
- python enumerate(),,欢迎关注本人博客:云
- (07)-Python3之--函数,,1.定义函数:实现了
- python--初始化实例属性,,初始化实例属性虽然我
- Python文件打开方式详解——r,w,a,r+,w+,a+,,r,r+,w,w+,
- Pyton项目打包成exe文件,,Python项目打包
- python socket编程入门级,,客户端import
评论关闭