来自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]))