4 回答

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
您應(yīng)該修改runInParallel以進(jìn)行可迭代的解包。
import time
from multiprocessing import Process
def worker(ii):
time.sleep(ii)
print("Working")
def runInParallel(*fns):
proc = []
for fn in fns:
func, *args = fn
p = Process(target=func, args=args)
p.start()
proc.append(p)
for p in proc:
p.join()
if __name__ == '__main__':
start = time.time()
runInParallel((worker, 2), (worker, 3), (worker, 5), (worker, 2))
print("Total time taken: ", time.time()-start)

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
worker這是因?yàn)楹椭g的差異worker()。前者是函數(shù),后者是函數(shù)調(diào)用。線路上發(fā)生的情況runInParallel(worker(2), worker(2), worker(2), worker(2))是,在開始執(zhí)行之前,所有四個(gè)調(diào)用runInParallel都已運(yùn)行。如果你print(fns)在開頭添加一個(gè),runInParallel你會(huì)看到一些不同。
快速解決:
def worker_caller():
worker(2)
和:
runInParallel(worker_caller, worker_caller, worker_caller, worker_caller)
這不是很方便,但主要是為了顯示問題所在。問題不在函數(shù)中worker。問題在于您混淆了傳遞函數(shù)和傳遞函數(shù)調(diào)用。如果您將第一個(gè)版本更改為:
runInParallel(worker(), worker(), worker(), worker())
那么你會(huì)遇到完全相同的問題。
但你可以這樣做:
runInParallel(lambda:worker(2), lambda: worker(2), lambda: worker(2), lambda: worker(2))
Lambda 非常有用。這是另一個(gè)版本:
a = lambda:worker(2)
b = lambda:worker(4)
c = lambda:worker(3)
d = lambda:worker(1)
runInParallel(a, b, c, d)

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
要傳遞參數(shù),您需要將它們傳遞給Process
構(gòu)造函數(shù):
p = Process(target=fn, args=(arg1,))

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
Process 構(gòu)造函數(shù)接受 args 和 kwargs 參數(shù),然后在執(zhí)行時(shí)將這些參數(shù)傳遞給進(jìn)程。文檔對此非常清楚。
所以你的代碼應(yīng)該修改如下:
def worker(ii):
time.sleep(ii)
print("Working")
def runInParallel(*fns):
proc = []
for fn in fns:
p = Process(target=fn, args=(2,))
p.start()
proc.append(p)
for p in proc:
p.join()
if __name__ == '__main__':
start = time.time()
runInParallel(worker, worker, worker, worker)
print("Total time taken: ", time.time()-start)
當(dāng)然,每個(gè)進(jìn)程的參數(shù)可能不同,您需要安排將正確的參數(shù)傳遞給 args(或關(guān)鍵字參數(shù)的 kwargs)中的每個(gè)進(jìn)程。這可以通過傳遞元組來實(shí)現(xiàn),例如runInParallel((worker,2), (worker,3), (worker,5), (worker,1),然后處理內(nèi)部的元組runInParallel。
添加回答
舉報(bào)