擅长:python、mysql、java
<p>我相信您要寻找的是<code>subprocess.terminate()</code>和{<cd2>}方法。在</p>
<p>您可能需要将其与注册到<a href="https://docs.python.org/2/library/atexit.html" rel="nofollow noreferrer">atexit</a>的方法结合起来,该方法会在子进程列表中迭代并终止它们。例如:</p>
<pre><code>def terminate_children(children):
for process in children:
process.terminate()
...
# Somewhere else in your code
children = [s.Popen(['python', 'test.py'], stdout=s.PIPE) for i in range(number_processes)] # Spools up number_processes child processes
atexit.register(terminate_children, children) # where children is a list of subprocesses
</code></pre>
<p>当您优雅地终止父进程时,这将优雅地终止所有子进程。在</p>
<p>如果您试图从一个完全独立的脚本中终止进程,而不直接引用子代码,请参见<a href="https://stackoverflow.com/questions/4214773/kill-process-with-python">here</a>。您基本上想看看python <code>os</code><a href="https://docs.python.org/2/library/os.html#process-management" rel="nofollow noreferrer">process management methods</a></p>