[英]python multiprocessing is not using multiple cores
Reading the documentation: https://docs.python.org/2/library/multiprocessing.html I decided to write a cpu intensive code and compare multiprocessing with serial computation. 阅读文档: https : //docs.python.org/2/library/multiprocessing.html我决定编写一个CPU密集型代码,并将多处理与串行计算进行比较。 First of all, if this library is using multiprocessing, then why I only see 1 python.exe process?
首先,如果此库使用的是多处理程序,那为什么我只看到1个python.exe进程? Secondly, why serial computation takes 12 seconds while multiprocessed one takes 22 seconds?
其次,为什么串行计算需要12秒,而多进程计算却需要22秒?
serial code: 串行码:
from datetime import datetime
def calc_fib(ind):
fb = 1
if ind >= 3:
prev = 1
i = 2
while i < ind:
prev_tmp = fb
fb += prev
prev = prev_tmp
i += 1
return fb
def long_calc_fib(ind):
val = 0
for j in range(500):
val = calc_fib(ind)
return val
if __name__ == "__main__":
t1 = datetime.now()
for i in range(10):
tmp = long_calc_fib(10000)
t2 = datetime.now()
print str(t2 - t1)
multiprocessing pool code: 多处理池代码:
from datetime import datetime
from multiprocessing.pool import ThreadPool
def calc_fib(ind):
fb = 1
if ind >= 3:
prev = 1
i = 2
while i < ind:
prev_tmp = fb
fb += prev
prev = prev_tmp
i += 1
return fb
def long_calc_fib(ind):
val = 0
for j in range(500):
val = calc_fib(ind)
return val
if __name__ == "__main__":
t1 = datetime.now()
pool = ThreadPool(processes=10)
async_results = []
for i in range(10):
async_results.append(pool.apply_async(long_calc_fib, (10000,)))
for res in async_results:
tmp = res.get()
t2 = datetime.now()
print str(t2 - t1)
My mistake. 我的错。 I must have used Pool instead of ThreadPool.
我必须使用Pool而不是ThreadPool。 By chaning ThreadPool to Pool, I reduced the time to 3 seconds.
通过将ThreadPool更改为Pool,我将时间减少到3秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.