python3导入joblib报错_python joblib并行在Windows上工作甚至“如果__name__ ==’__ main__’:”被添加...

根据

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", line 258, in _bootstrap

self.run()

File "C:\Python27\lib\multiprocessing\process.py", line 114, in run

self._target(*self._args, **self._kwargs)

File "C:\Python27\lib\multiprocessing\pool.py", line 102, 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 sqrt

def 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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值