Difference between map, apply, map_async, apply_async(Python multiprocessing.Pool)
在multiprocessing.Pool中有4种方法可以用来将任务分给到各个子进程,分别是apply,apply_async,map,map_async,4种方法的对比请见下表(原文中的表):
| Multi-args | Concurrence | Blocking | Ordered-results | |
|---|---|---|---|---|
| apply | YES | NO | YES | NO |
| apply_async | YES | YES | NO | NO |
| map | NO | YES | YES | YES |
| map_async | NO | YES | NO | YES |
- 需要说明的是,最后一列
Ordered-results表示各个方法返回的结果是否是有序的。也就是说(以下面的程序为例)不是表示程序中print语句的执行顺序是否有序(print的结果不一定有序),而是表示各个函数的返回值是否是按照调用的顺序返回的。 - Note that
mapandmap_asyncare called for a list of jobs in one time, butapplyandapply_asynccan only called for one job. However,apply_asyncexecute a job in background therefore in parallel.
对4种方法分别进行了下面的测试,测试代码如下:
1 | def func(num): |
代码运行结果如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24apply ----------------------------------------
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 # 不保证有序
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # 不保证有序
apply time cost: 0.13375353813171387
************************************************************
apply_async ----------------------------------------
20 19 18 17 16 14 13 15 12 11 10 9 7 6 8 5 4 3 2 1 # 无序
[20, 18, 19, 16, 17, 14, 13, 12, 11, 10, 9, 7, 6, 5, 4, 3, 15, 2, 8, 1] # 无序
apply_async time cost: 0.10675978660583496
************************************************************
map ----------------------------------------
20 19 17 16 18 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 # 无序
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # 有序
map: 0.10820269584655762
************************************************************
map_async ----------------------------------------
20 19 17 18 16 15 13 14 10 12 9 8 7 6 11 5 4 3 2 1 # 无序
[[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]] # 有序
map_async: 0.11021280288696289
************************************************************
end