0
首先我用ProcessPoolExecutor
這樣的:python使用ProcessPoolExecutor easiliy?
def s(i):
time.sleep(1)
return i
futs = []
with ProcessPoolExecutor(max_workers=10) as executor:
for i in range(WORKER_CNT):
print executor.submit(s, i,).result()
但後來我發現這是不平行的,然後我改成這樣:
def s(i):
time.sleep(1)
return i
futs = []
with ProcessPoolExecutor(max_workers=10) as executor:
for i in range(WORKER_CNT):
futs.append(executor.submit(s, i,))
for f in futs:
print f.result()
之後我就開始擔心WORKER_CNT
可太大,我需要等待多少時間看最後的結果,所以我改成這樣:
def s(i):
time.sleep(1)
return i
futs = []
with ProcessPoolExecutor(max_workers=10) as executor:
for i in range(WORKER_CNT):
futs.append(executor.submit(s, i,))
if len(futs) == 10:
for f in futs:
print f.result()
futs = []
這個時候,我可以處理10
每次,我想知道我能做到這一點嗎?