【发布时间】:2013-09-11 08:46:41
【问题描述】:
我正在试验 Python 3.2 中引入的新的闪亮 concurrent.futures 模块,我注意到,几乎使用相同的代码,使用来自 concurrent.futures 的 Pool 比使用 Pool 慢方式 multiprocessing.Pool.
这是使用多处理的版本:
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:
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))
使用来自 Eli Bendersky article 的简单分解函数,这些是在我的计算机(i7、64 位、Arch Linux)上的结果:
[juanlu@nebulae]─[~/Development/Python/test]
└[10:31:10] $ time python pool_multiprocessing.py
real 0m10.330s
user 1m13.430s
sys 0m0.260s
[juanlu@nebulae]─[~/Development/Python/test]
└[10:31:29] $ time python pool_futures.py
real 4m3.939s
user 6m33.297s
sys 0m54.853s
我无法使用 Python 分析器分析这些,因为我遇到了 pickle 错误。有什么想法吗?
【问题讨论】:
-
你能发布更新吗?也许是 3.8 版?
标签: python concurrency multiprocessing future concurrent.futures