from joblib import Parallel,delayeddef f(x): return sqrt(x)if __name__ == '__main__': a = Parallel(n_jobs=2)(delayed(f)(i) for i in range(10))
这是错误消息:
Process PoolWorker-2: Process PoolWorker-1: Traceback (most recent call last): file "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\multiprocessing\process.py",line 258,in _bootstrapself.run() file "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\multiprocessing\process.py",line 114,in runself._target(*self._args,**self._kwargs) file "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\multiprocessing\pool.py",line 102,in workertask = get() file "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\User\lib\site-packages\joblib\pool.py",line 363,in getreturn recv() AttributeError: 'module' object has no attribute 'f'解决方法 根据 this site,问题是windows特定的:
Yes: under linux we are forking,thus their is no need to pickle the
function,and it works fine. Under windows,the function needs to be
pickleable,IE it needs to be imported from another file. This is
actually good practice: making modules pushes for reuse.
我已经尝试过你的代码,它在linux下运行完美.
在windows下,如果从脚本运行,它运行正常,如python script_with_your_code.py.但是在交互式python会话中运行时失败了.当我将f函数保存在单独的模块中并将其导入到我的交互式会话中时,它对我有用.
不工作:
互动环节:
>>> from math import sqrt>>> from joblib import Parallel,delayed>>> def f(x):... return sqrt(x)>>> if __name__ == '__main__':... a = Parallel(n_jobs=2)(delayed(f)(i) for i in range(10))...Process PoolWorker-1:Traceback (most recent call last): file "C:\Python27\lib\multiprocessing\process.py",in _bootstrap self.run() file "C:\Python27\lib\multiprocessing\process.py",in run self._target(*self._args,**self._kwargs) file "C:\Python27\lib\multiprocessing\pool.py",in worker task = get() file "C:\Python27\lib\site-packages\joblib\pool.py",line 359,in get return recv()AttributeError: 'module' object has no attribute 'f'
工作方式:
fun.py
from math import sqrtdef f(x): return sqrt(x)
互动环节:
>>> from joblib import Parallel,delayed>>> from fun import f>>> if __name__ == '__main__':... a = Parallel(n_jobs=2)(delayed(f)(i) for i in range(10))...>>> a[0.0,1.0,1.4142135623730951,1.7320508075688772,2.0,2.23606797749979,2.449489742783178,2.6457513110645907,2.8284271247461903,3.0]总结
以上是内存溢出为你收集整理的python joblib并行在Windows上工作甚至“如果__name__ ==’__ main__’:”被添加全部内容,希望文章能够帮你解决python joblib并行在Windows上工作甚至“如果__name__ ==’__ main__’:”被添加所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)