来自documentaion的示例在Jupiter Notebook中不起作用 [英] Example from documentaion doesn't work in Jupiter Notebook

查看:56
本文介绍了来自documentaion的示例在Jupiter Notebook中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这份文献.还有一个例子

I had looked at the documentaion. And there was an example

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

问题是:它不起作用.我在Jupiter Notebook单元中运行此代码.而且该单元格不会引发任何异常.但是木星的航站楼确实有.它说:AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>

The problem is: it is not working. I run this code in Jupiter Notebook cell. And this the cell doesn't raise any exception. But Jupiter's terminal does. And it says: AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>

此处所写的问题可能是因为我不使用__name__ == '__main__'条件.但是我知道.

As written here the problem may be because I don't use __name__ == '__main__' condition. But I do.

我确实从文档中复制并粘贴了示例,但它不起作用.我该怎么办?

I had literally copy and paste example from the documention and it's not working. What should I do?

推荐答案

我怀疑您正在Windows上运行.如果是这样,这是一个已知问题.参见

I suspect you are running on Windows. If so, this is a known issue. See this article. You need to add your function f to a file, such as worker.py:

worker.py

def f(x):
    return x*x

然后,您的jupyter笔记本代码将变为:

Then you jupyter notebook code becomes:

from multiprocessing import Pool
import worker


if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(worker.f, [1, 2, 3]))

这篇关于来自documentaion的示例在Jupiter Notebook中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆