[英]Example from documentaion doesn't work in 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]))
The problem is: it is not working.问题是:它不起作用。 I run this code in Jupiter Notebook cell.
我在 Jupiter Notebook 单元中运行此代码。 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)>
它说:
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
As written here the problem may be because I don't use __name__ == '__main__'
condition.正如这里所写的,问题可能是因为我不使用
__name__ == '__main__'
条件。 But I do.但是我愿意。
I had literally copy and paste example from the documention and it's not working.我从文档中复制并粘贴了示例,但它不起作用。 What should I do?
我该怎么办?
I suspect you are running on Windows.我怀疑你在 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
:您需要将函数
f
添加到文件中,例如worker.py
:
worker.py工人.py
def f(x):
return x*x
Then you jupyter notebook code becomes:然后你的 jupyter notebook 代码变成了:
from multiprocessing import Pool
import worker
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(worker.f, [1, 2, 3]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.