Using subprocess.Popen for Process with Large Output

后端 未结 7 1981
礼貌的吻别
礼貌的吻别 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:46

    I had the same problem. If you have to handle a large output, another good option could be to use a file for stdout and stderr, and pass those files per parameter.

    Check the tempfile module in python: https://docs.python.org/2/library/tempfile.html.

    Something like this might work

    out = tempfile.NamedTemporaryFile(delete=False)
    

    Then you would do:

    Popen(... stdout=out,...)
    

    Then you can read the file, and erase it later.

提交回复
热议问题