python多进程Pool.map代码执行时报错
我的机器是win7 64位,python版本2.7,想写一段代码,比较一下本机上多进程和多线程的执行效率,在跑并行执行20次1-10万的乘法运算的代码时执行失败,请帮忙看应该怎么修改:
import multiprocessing
from multiprocessing import Pool
from datetime import datetime
def cal_sum_not_so_big():
i=1
sum=0
while i <= 100000000:
sum+=i
i+=1
return 'sum is %s ' % sum
cal_a=cal_multi_not_so_big
def run_and_log(function,name,*options):
start=datetime.now()
print '%s instance %s starts at %s' % (function,name,start)
function(*options)
end=datetime.now()
print '%s instance %s ends at %s,cost %s' % (function,name,start,end-start)
def scen2_full_proc(func):
cpu_number=multiprocessing.cpu_count()
pool=Pool(cpu_number)
opt1=[]
i=1
while i <=20:
opt1.append((func,i,))
i+=1
start=datetime.now()
print 'full proc test start at %s' % start
pool.map(run_and_log,opt1)
end=datetime.now()
print 'full proc test end at %s ,cost %s' % (end,end-start,)
if __name__ == "__main__":
multiprocessing.freeze_support()
scen2_full_proc(cal_a)
报错:
Traceback (most recent call last):
File "E:/dev/python/trackRF/threadpools/multicompare.py", line 93, in <module>
scen2_full_proc(cal_a)
File "E:/dev/python/trackRF/threadpools/multicompare.py", line 75, in scen2_full_proc
pool.map(run_and_log,opt1)
File "C:\Python27\lib\multiprocessing\pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "C:\Python27\lib\multiprocessing\pool.py", line 558, in get
raise self._value
TypeError: run_and_log() takes at least 2 arguments (1 given)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
map函数只支持一个参数的函数,需要改造run_and_log函数只接受一个参数,比如: