擅长:python、mysql、java
<p>等待响应不超过一秒钟:</p>
<pre><code>from subprocess import Popen, PIPE
p = Popen(['command', 'the first argument', 'the second one', '3rd'],
stdin=PIPE, stdout=PIPE, stderr=PIPE,
universal_newlines=True)
out, err = p.communicate('\n', timeout=1) # write newline
</code></pre>
<p>通过3.2+子进程模块的<a href="http://pypi.python.org/pypi/subprocess32/" rel="nofollow noreferrer">http://pypi.python.org/pypi/subprocess32/</a>后端口,python2.x上可以使用超时特性。见<a href="https://stackoverflow.com/a/12698328/4279">subprocess with timeout</a>。在</p>
<p>对于使用线程的解决方案,信号报警,选择,iocp,twisted,或者只是一个临时文件,请参阅您的问题下面的相关帖子的链接。在</p>