这可能已被问到,但我找不到任何有关使用 python 2.7 时 subprocess.call 超时的信息
最佳答案
我在 2.7 中一直使用的一种简单方法是利用 subprocess.poll()
和 time.sleep()
延迟。这是一个非常基本的示例:
import subprocess
import time
x = #some amount of seconds
delay = 1.0
timeout = int(x / delay)
args = #a string or array of arguments
task = subprocess.Popen(args)
#while the process is still executing and we haven't timed-out yet
while task.poll() is None and timeout > 0:
#do other things too if necessary e.g. print, check resources, etc.
time.sleep(delay)
timeout -= delay
如果您设置 x = 600
,那么您的超时时间将达到 10 分钟。而 task.poll()
将查询进程是否已终止。在这种情况下,time.sleep(delay)
将休眠 1 秒,然后将超时时间减少 1 秒。您可以随心所欲地玩那个部分,但基本概念始终是相同的。
希望这对您有所帮助!
subprocess.poll()
https://docs.python.org/2/library/subprocess.html#popen-objects
关于python - 如何使用 python 2.7.6 使 subprocess.call 超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23287689/