Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

python - Multiprocessing program has AttributeError in Anaconda notebook

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

The program:

from multiprocessing import Process, freeze_support

def f():
    print ('hello world!')

if __name__ == '__main__':
    #freeze_support()
    Process(target=f).start()

gives the following error:

[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)  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

"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 multiprocessing apply_async never returns result on Windows 7

Hope it is a useful information to you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...