apply、apply_async、map、map_async四种方法的对比:
Multi-args(多参数) Concurrence(并发) Blocking(阻塞) Ordered-results(返回结果顺序)
map no yes yes yes
apply yes no yes yes
map_async no yes no no
apply_async yes yes no no
注以下例子皆为多参数下的使用情况及耗时情况:
apply使用:
import time
from multiprocessing import Pool, Process
def worker(x ,y):
result = x * y
time.sleep(1.5)
return result
if __name__ == '__main__':
start = time.time()
result_list = []
pool = Pool(processes=4)
for i in range(10):
result = pool.apply(worker, args= (i, i))
result_list.append(result)
pool.close()
pool.join()
end = time.time()
print(end-start)
结果:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
15.509403467178345
apply_async使用:
import time
from multiprocessing import Pool, Process
def worker(x ,y):
result = x * y
time.sleep(1.5)
return result
def collect_result(result):
result_list.append(result)
if __name__ == '__main__':
start = time.time()
result_list = []
pool = Pool(processes=4)
for i in range(10):
result = pool.apply_async(worker, args= (i, i), callback=collect_result)
pool.close()
pool.join()
end = time.time()
print(end-start)
结果:
[1, 0, 4, 9, 25, 16, 36, 49, 81, 64]
4.955698490142822
map使用:
import time
from multiprocessing import Pool, Process
def worker(z):
result = z[0] * z[1]
time.sleep(1.5)
return result
if __name__ == '__main__':
start = time.time()
result_list = []
pool = Pool(processes=4)
for i in range(10):
result = pool.map(worker, ([(i, i)]))
result_list.append(result)
pool.close()
pool.join()
print(result_list)
end = time.time()
print(end-start)
结果:
[[0], [1], [4], [9], [16], [25], [36], [49], [64], [81]]
15.50440001487732
map_async使用:
import time
from multiprocessing import Pool, Process
def worker(z):
result = z[0] * z[1]
time.sleep(1.5)
return result
def collect_result(result):
result_list.append(result)
if __name__ == '__main__':
start = time.time()
result_list = []
pool = Pool(processes=4)
for i in range(10):
result = pool.map_async(worker, ([(i, i)]), callback=collect_result)
pool.close()
pool.join()
print(result_list)
end = time.time()
print(end-start)
结果:
[[0], [9], [1], [4], [16], [36], [49], [25], [64], [81]]
4.948090314865112