Getting realtime output using subprocess

前端 未结 18 2792
执笔经年
执笔经年 2020-11-22 10:12

I am trying to write a wrapper script for a command line program (svnadmin verify) that will display a nice progress indicator for the operation. This requires me to be abl

18条回答
  •  悲&欢浪女
    2020-11-22 10:53

    Depending on the use case, you might also want to disable the buffering in the subprocess itself.

    If the subprocess will be a Python process, you could do this before the call:

    os.environ["PYTHONUNBUFFERED"] = "1"
    

    Or alternatively pass this in the env argument to Popen.

    Otherwise, if you are on Linux/Unix, you can use the stdbuf tool. E.g. like:

    cmd = ["stdbuf", "-oL"] + cmd
    

    See also here about stdbuf or other options.

    (See also here for the same answer.)

提交回复
热议问题