(1)p1 = multiprocessing.Process(test1)
p2 = multiprocessing.Process(target=test2)
錯誤: p1缺少target,應為(target=test1)
Traceback (most recent call last):
File "D:/untitled/多進程練習.py", line 11, in <module>
p1 = multiprocessing.Process(test1)
File "D:\python\lib\multiprocessing\process.py", line 74, in __init__
assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now
(2) 在有進程程序中,沒有代碼:if __name__ == __main__: 會出現如下錯誤
RuntimeError: RuntimeError
if __name__ == '__main__': 的作用:防止被被其他文件導入時顯示多余的程序主體部分
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.:
is not going to be frozen to produce an executable.:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
is not going to be frozen to produce an executable.
子進程會在運行時拷貝當前主進程中的所有內容,這也就意味着當一個新的子進程被創建的時候,該子進程就會復制當前模塊,當然也包括了以下兩行:
multiprocessing.Process(target=test1).start()
multiprocessing.Process(target=test).start()
multiprocessing.Process(target=test).start()
很顯然,這樣的寫法可能形成無限遞歸式地創建新的子進程。所以為了避免以上情況發生,我們在此引入了 if __name__ == __main__: 。