2017-08-29 190 views
1

在计算器的前面的讨论:python多处理模块中的map(block)和map_async(非block)有什么不同?

python-multiprocessing-map-vs-map-async

由于quikst3r说:你会注意到地图会顺序执行,但map_async没有。

这是我的地图(块)vs map_async(非块)的例子。

map-sync.py代码。

import multiprocessing 
import os 
import time 
from datetime import datetime 

def subprocess(number): 
    print('this is the %d th subprocess' % number) 
    time.sleep(3) 

def mainprocess(): 
    print('this is the main process ,process number is : %d' % os.getpid()) 
    pool = multiprocessing.Pool(3) 
    list=range(9) 
    pool.map(subprocess,list) 
    pool.close() 
    pool.join() 

if __name__ == '__main__': 
    mainprocess() 

Sometimes map_sync can not execute in order.
有时map_sync不能为了执行在地图的功能在这里。

map-async.py代码。

import multiprocessing 
import os 
import time 
from datetime import datetime 

def subprocess(number): 
    print('this is the %d th subprocess' % number) 
    time.sleep(3) 

def mainprocess(): 
    print('this is the main process ,process number is : %d' % os.getpid()) 
    pool = multiprocessing.Pool(3) 
    list=range(9) 
    pool.map_async(subprocess,list) 
    pool.close() 
    pool.join() 

if __name__ == '__main__': 
    mainprocess() 

Sometimes map_async can execute in order. 有时map_async可以为了执行在这里为map_async功能。

对于多进程,所有进程都是抢先式多任务处理,无需执行map和map_async命令。

地图和map_async没有绝对执行,运行时间几乎相同--9秒(3 * 3 = 9)。

我们来看看多处理模块应用功能块。

apply.py代码。

import multiprocessing 
import os 
import time 
from datetime import datetime 

def subprocess(number): 
    print('this is the %d th subprocess' % number) 
    time.sleep(3) 

def mainprocess(): 
    print('this is the main process ,process number is : %d' % os.getpid()) 
    pool = multiprocessing.Pool(3) 
    for i in range(9):  
     pool.apply(subprocess,args=(i,)) 
    pool.close() 
    pool.join() 

if __name__ == '__main__': 
    mainprocess() 

enter image description here

27 = 3 * 9(所有进程阻塞)

我困惑如何证明在多处理模块地图和map_async之间的块和非块归因?
map(块)和map_async(非块)多处理模块之间有什么区别?

回答

1

也许你误解了quikst3r的话。

池创建许多工作进程来做某件事情,关于是否阻塞,它表示工作进程是否会阻塞主进程。并且map将阻止主进程util完成其工作,并且map_asyc不会阻塞主进程,所以在map_async中,所有进程都将同时进行。他们不确保工作流程的顺序。

import multiprocessing 
import os 
import time 
from datetime import datetime 

def subprocess(number): 
    print('this is the %d th subprocess' % number) 
    time.sleep(3) 

def mainprocess(): 
    print('this is the main process ,process number is : %d' % os.getpid()) 
    pool = multiprocessing.Pool(3) 
    list=range(9) 
    pool.map(subprocess,list) # or map_aysnc                                        
    print "hallo" 
    print "hallo again" 
    pool.close() 
    pool.join() 

if __name__ == '__main__': 
    mainprocess() 

如果是地图,输出将是这样的:

this is the main process ,process number is : 24812 
this is the 0 th subprocess 
this is the 1 th subprocess 
this is the 2 th subprocess 
this is the 3 th subprocess 
this is the 4 th subprocess 
this is the 5 th subprocess 
this is the 6 th subprocess 
this is the 7 th subprocess 
this is the 8 th subprocess 
hallo 
hallo again 

而如果是map_async,输出为:

this is the main process ,process number is : 24775 
hallo 
hallo again 
this is the 0 th subprocess 
this is the 1 th subprocess 
this is the 2 th subprocess 
this is the 3 th subprocess 
this is the 4 th subprocess 
this is the 5 th subprocess 
this is the 6 th subprocess 
this is the 7 th subprocess 
this is the 8 th subprocess 

至于申请或apply_async,这是关于块的含义与map或map_async相同。这是说工人进程阻止了主流程。

相关问题