問題描述
我正在 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.
此包中的功能要求 ma??in 模塊必須可由子模塊導入.這在編程指南中有所介紹,但在此值得指出.這意味著一些示例,例如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 上的結果
希望對你有用.
這篇關于多處理程序在 Anaconda 筆記本中有 AttributeError的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!