【问题标题】:Memory Issues with Python/Jython subprocess modulePython/Jython 子进程模块的内存问题
【发布时间】: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


    【解决方案1】:

    您可以在“time.sleep(5)”之前尝试显式“rc.terminate()”。除此之外,我建议分析和/或转储堆以查看内存的使用位置。我认为communicate() 写入的缓冲区正在填满。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-24
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      相关资源
      最近更新 更多