<p>您应该添加这一行<code>if __name__ == '__main__':</code>,以使事情正常工作</p>
<p>说明:
您的主脚本将由<code>process.py</code>模块导入,然后它将执行您的脚本行2次,一次在导入期间执行,另一次在脚本执行期间执行</p>
<p>如果我们没有包括<code>if __name__ == '__main__':</code>,下面是运行时错误</p>
<pre><code>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.
</code></pre>
<p>python 3.6中的工作代码是:</p>
<pre><code>from multiprocessing import Process
class MyProcess(Process):
def run(self):
## do someting
print ('finished')
processes = []
if __name__ == '__main__':
for i in range(3):
p = MyProcess()
p.start()
processes.append(p)
for p in processes:
p.join()
print('we are done here .......')
</code></pre>
<p>输出:</p>
<pre><code>finished
finished
finished
we are done here .......
</code></pre>