私は新しい光沢のあるconcurrent.futuresモジュールをPython 3.2で導入して実験していましたが、ほぼ同じコードで、concurrent.futuresのプールを使用していることがわかりましたwayは、multiprocessing.Poolを使用するよりも遅いです。ProcessPoolExecutorはconcurrent.futuresからマルチプロセスより遅いです。プール
これはマルチプロセッシングを使用したバージョンです:
def hard_work(n):
# Real hard work here
pass
if __name__ == '__main__':
from multiprocessing import Pool, cpu_count
try:
workers = cpu_count()
except NotImplementedError:
workers = 1
pool = Pool(processes=workers)
result = pool.map(hard_work, range(100, 1000000))
そして、これがconcurrent.futures使用している:このEli Bendersky articleから取られたナイーブ因数分解機能を使用して
def hard_work(n):
# Real hard work here
pass
if __name__ == '__main__':
from concurrent.futures import ProcessPoolExecutor, wait
from multiprocessing import cpu_count
try:
workers = cpu_count()
except NotImplementedError:
workers = 1
pool = ProcessPoolExecutor(max_workers=workers)
result = pool.map(hard_work, range(100, 1000000))
を、これらは私のコンピュータ上の結果であります(i7,64ビット、Arch Linux):
[[email protected]]─[~/Development/Python/test]
└[10:31:10] $ time python pool_multiprocessing.py
real 0m10.330s
user 1m13.430s
sys 0m0.260s
[[email protected]]─[~/Development/Python/test]
└[10:31:29] $ time python pool_futures.py
real 4m3.939s
user 6m33.297s
sys 0m54.853s
私はpickleエラーが発生するので、私はPythonプロファイラでこれらをプロファイルできません。何か案は?
私は、あなたの命名規則、特に 'workers'と' hard_work'を愛しています:P –
Cool、innit? :P – astrojuanlu