almost 3 years ago

Consider concurrent.futures for True Parallelism

使用 concurrent.futures 裡頭的 ProcessPoolExecutor 可以很簡單地平行處理 CPU-bound 的程式,省得用 multiprocessing 自幹。

from concurrent.futures import ProcessPoolExecutor

start = time()
pool = ProcessPoolExecutor(max_workers=2)  # The one change

results = list(pool.map(gcd, numbers))
end = time()
print('Took %.3f seconds' % (end - start))
← Effective Python 心得筆記: Item 40 Effective Python 心得筆記: Item 42 →
 
comments powered by Disqus