多处理程序在 Anaconda 笔记本中有 AttributeError
问题描述
我正在 Windows 7 64 位上运行一个简单的Hello World"程序,其规格如下:
I am running a simple "Hello World" program on Windows 7 64 bit with the following specifications:
Python 3.4.3 | Anaconda 2.3.0 (64-bit) | [MSC v.1600 64 bit (AMD64)] IPython 4.0.0
程序:
from multiprocessing import Process, freeze_support
def f():
print ('hello world!')
if __name__ == '__main__':
#freeze_support()
Process(target=f).start()
给出以下错误:
[I 15:02:23.855 NotebookApp] Saving file at /uhc/FeatureContributionToK-meansClu
sterWithPC.ipynb
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Anaconda3libmultiprocessingspawn.py", line 106, in spawn_main
exitcode = _main(fd)
File "C:Anaconda3libmultiprocessingspawn.py", line 116, in _main
self = pickle.load(from_parent)
AttributeError: Can't get attribute 'f' on module '__main__' (built-in)
解决方案
这是因为多处理在交互式解释器中不能很好地工作.主要原因是windows中没有适用的fork()函数.他们的网页上对此进行了解释.
This is because of the fact that multiprocessing does not work well in the interactive interpreter. The main reason is that there is no fork() function applicable in windows. It is explained on their web page itself.
此包中的功能要求 main 模块必须可由子模块导入.这在编程指南中有所介绍,但在此值得指出.这意味着一些示例,例如multiprocessing.Pool 示例在交互式解释器中不起作用."
"Functionality within this package requires that the main module must be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter."
https://docs.python.org/2/library/multiprocessing.html#windows
如果您在多处理中使用池函数,也会出现同样的问题.在这篇文章中解决了.因此,您可以使用该方法来执行您的并行处理想法.
This same problem will come if you are using pool function in multiprocessing. It is solved in this post . You can hence use that method for executing your idea of parallel processing.
Python 多处理 apply_async 永不返回Windows 7 上的结果
希望对你有用.
相关文章