擅长:python、mysql、java
<p>这对我有用</p>
<pre><code>import subprocess
import time
for _ in range(999999): # or whatever
p1 = subprocess.Popen(['exe_file', 'arg1', 'arg2'])
time.sleep(0.1) # wait a small amount of time, hopefully the process ends fast
return_code = p1.poll()
if return_code is not None: # we're happy, the process ended fast
... # do something to celebrate
else: # we're sad. let the process run for at most 5 seconds
time.sleep(5)
p1.terminate() # this kills the process. try p1.kill() too...
p1.wait() # this cleans up the process registry.
</code></pre>
<p>免责声明:这是linux代码。windows上的情况可能有所不同,但您可以先检查一下,然后在这里阅读<a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">https://docs.python.org/2/library/subprocess.html</a>关于子进程库以及linux和windows之间的区别的内容。</p>