Using subprocess.Popen for Process with Large Output

后端 未结 7 1978
礼貌的吻别
礼貌的吻别 2020-12-02 16:56

I have some Python code that executes an external app which works fine when the app has a small amount of output, but hangs when there is a lot. My code looks like:

7条回答
  •  鱼传尺愫
    2020-12-02 17:36

    A lot of output is subjective so it's a little difficult to make a recommendation. If the amount of output is really large then you likely don't want to grab it all with a single read() call anyway. You may want to try writing the output to a file and then pull the data in incrementally like such:

    f=file('data.out','w')
    p = subprocess.Popen(cmd, shell=True, stdout=f, stderr=subprocess.PIPE)
    errcode = p.wait()
    f.close()
    if errcode:
        errmess = p.stderr.read()
        log.error('cmd failed <%s>: %s' % (errcode,errmess))
    for line in file('data.out'):
        #do something
    

提交回复
热议问题