【发布时间】:2020-11-12 17:03:08
【问题描述】:
我正在运行一个带有 jython 脚本的子进程。我没有收到返回码,而是必须在标准输出中扫描带有超时错误号的字符串。
我发现这非常慢,并且不确定是否有更好的方法来解决它。我尝试在time.sleep(5) 之前将指向rc, out, err 变量的指针归零,并尝试增加time.sleep(30)。我还能尝试什么来减少内存使用量?
import subprocess
cmd = 'path/to/some/other/process'
tries = 3
def timeout_check(out, tries):
for i in range(tries):
rc = subprocess.Popen(cmd, stdout=STDOUT, stderr=Err)
out, err = rc.communicate()
if 'Error 1234: Timeout Error Message' not in out:
if rc.returncode == 0:
if out and len(out) > 0:
print(out)
break
else:
if out and len(out) > 0:
print(out)
raise Exception('The process failed with non-zero return code')
else:
print('Timeout! Will re-try...')
time.sleep(5)
else:
raise Exception('Cannot start process, tried %i times' % tries)
【问题讨论】:
标签: python subprocess jython