報錯內容如下:
RuntimeError:
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:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
根據報錯提示可知:
沒有在模塊中使用
if __name__ == '__main__':
freeze_support()
如果程序需要,可以省略“freeze_support()”行
更改代碼如下:可以正常運行
# 進程
from multiprocessing import Process
def foo(i):
print(" This is Process ", i)
def main():
for i in range(5):
p=Process(target=foo, args=(i, ))
p.start()
if __name__ == '__main__':
main()
輸出結果:
This is Process 1
This is Process 0
This is Process 2
This is Process 3
This is Process 4