擅长:python、mysql、java
<pre><code>import subprocess as sub
import threading
class RunCmd(threading.Thread):
def __init__(self, cmd, timeout):
threading.Thread.__init__(self)
self.cmd = cmd
self.timeout = timeout
def run(self):
self.p = sub.Popen(self.cmd)
self.p.wait()
def Run(self):
self.start()
self.join(self.timeout)
if self.is_alive():
self.p.terminate()
self.join()
RunCmd(["./someProg", "arg1"], 60).Run()
</code></pre>
<hr/>
<p>引自:<a href="https://stackoverflow.com/questions/4158502/python-kill-or-terminate-subprocess-when-timeout">Python: kill or terminate subprocess when timeout</a></p>