pool.map()函數(shù)拟枚,有兩個(gè)參數(shù)可以傳,第一個(gè)參數(shù)傳的是函數(shù)吼渡,第二個(gè)參數(shù)傳的是數(shù)據(jù)列表容为。
那么怎么在第二個(gè)數(shù)據(jù)列表,多傳幾個(gè)參數(shù)呢寺酪,方法是通過(guò)對(duì)有多個(gè)參數(shù)的方法進(jìn)行封裝坎背,在進(jìn)程中運(yùn)行封裝后的方法。
類似如下調(diào)用job1()方法:
# -*- coding:utf-8 -*-
import time
import multiprocessing
def job(x ,y):
"""
:param x:
:param y:
:return:
"""
return x * y
def job1(z):
"""
:param z:
:return:
"""
return job(z[0], z[1])
if __name__ == "__main__":
time1=time.time()
pool = multiprocessing.Pool(2)
data_list=[(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10)]
res = pool.map(job1,data_list)
time2=time.time()
print(res)
pool.close()
pool.join()
print('總共耗時(shí):' + str(time2 - time1) + 's')