这是Python代码,用于在超时的情况下运行给定命令并返回其stdout
def run_python2_file_with_args(*args, **kwargs):
timeout = kwargs.pop('timeout', 500)
from subprocess import Popen, PIPE
from threading import Timer
cmd = ' '.join(str(arg) for arg in args)
print(cmd)
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
print(timeout)
timer = Timer(timeout, proc.kill)
try:
timer.start()
stdout, stderr = proc.communicate()
finally:
timer.cancel()
print(stdout)
这对于超时工作正常,但是如果在Popen计时器中使用“ shell = True”,超时后不会终止进程。有人可以帮我吗