我試圖運行一個函數與多處理。這是代碼:蟒蛇多處理找不到錯誤
import multiprocessing as mu
output = []
def f(x):
output.append(x*x)
jobs = []
np = mu.cpu_count()
for n in range(np*500):
p = mu.Process(target=f, args=(n,))
jobs.append(p)
running = []
for i in range(np):
p = jobs.pop()
running.append(p)
p.start()
while jobs != []:
for r in running:
if r.exitcode == 0:
try:
running.remove(r)
p = jobs.pop()
p.start()
running.append(p)
except IndexError:
break
print "Done:"
print output
輸出是[],而它應該是[1,4,9,...]。有人看到我犯了一個錯誤?
Cursory瞥了一下代碼,你在哪裏等待工作完成? 'jobs.join()'在哪裏?似乎也缺乏鎖定,但我還沒有弄清楚你的邏輯。 – cdarke
用while和if if r.exitcode == 0,我認爲 –
@cdarke是什麼? 'None!= 0' – jonrsharpe