我在子进程轮询中遇到问题,当进程完成时不返回返回代码。
I found out how to set a timeout on subprocess.Popen,并以此作为我代码的基础。但是,我有一个使用Java的调用无法正确报告返回代码,因此即使实际上已完成,每个调用也会“超时”。我知道该过程已经完成,因为删除轮询超时检查后,该调用将运行,并且不会在返回时限内返回良好的退出代码。
这是我正在测试的代码。
import subprocess
import time
def execute(command):
print('start command: {}'.format(command))
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print('wait')
wait = 10
while process.poll() is None and wait > 0:
time.sleep(1)
wait -= 1
print('done')
if wait == 0:
print('terminate')
process.terminate()
print('communicate')
stdout, stderr = process.communicate()
print('rc')
exit_code = process.returncode
if exit_code != 0:
print('got bad rc')
if __name__ == '__main__':
execute(['ping','-n','15','127.0.0.1']) # correctly times out
execute(['ping','-n','5','127.0.0.1']) # correctly runs within the time limit
# incorrectly times out
execute(['C:\\dev\\jdk8\\bin\\java.exe', '-jar', 'JMXQuery-0.1.8.jar', '-url', 'service:jmx:rmi:///jndi/rmi://localhost:18080/jmxrmi', '-json', '-q', 'java.lang:type=Runtime;java.lang:type=OperatingSystem'])
您可以看到有两个示例是为了超时而设计的,有两个不是超时的,它们都正常工作。但是,最后一个(使用jmxquery获取tomcat指标)不会返回退出代码,因此“超时”并必须终止,这将导致其返回错误代码1。
子进程轮询与此Java进程进行交互的方式是否导致其不返回退出代码,我是否缺少某些东西?是否有一种方法可以使用超时选项?
我在子流程轮询中遇到问题,当流程完成时不返回返回代码。我了解了如何在subprocess.Popen上设置超时,并将其用作代码的基础。 ...
这与许多现有问题的起因相同,但强加超时的要求需要不同的答案。