python多处理模块中的map(块)和map_async(非块)之间有什么区别?

python多处理模块中的map(块)和map_async(非块)之间有什么区别?,python,multiprocessing,Python,Multiprocessing,在前面的stackoverflow讨论中: 正如quikst3r所说:您会注意到map将按顺序执行,但map\u async没有 下面是我的map(块)与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'

在前面的stackoverflow讨论中:

正如quikst3r所说:您会注意到map将按顺序执行,但map\u async没有

下面是我的map(块)与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()
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()

有时map_sync无法在此处为map功能执行命令

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(subprocess,list)
    pool.close()
    pool.join()

if __name__ == '__main__':
    mainprocess()
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()
有时,这里可以执行map\u async以实现map\u async函数

对于多处理,所有进程都是抢占式多任务,map和map\u异步没有顺序

map和map_async没有绝对执行顺序,运行时间几乎相同——9秒(3*3=9)

让我们看看多处理模块的apply函数中的块

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()

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

我对如何在多处理模块中演示map和map\u async之间的块和非块属性感到困惑?

map(块)和map_异步(非块)多处理模块之间有什么区别?

也许您误解了quikst3r的话

池创建多个工作进程来做一些事情,关于块与否,这意味着工作进程是否会阻止主进程。map将阻止主进程直到它完成它的工作,而map\u asyc不会阻止主进程,因此在map\u 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()
如果是map,则输出如下:

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或apply\u async,其关于block的含义与map或map\u async相同。也就是说,辅助进程是否阻止了主进程