(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
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__: 。