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)

评论关闭