Getting progress message from a subprocess

后端 未结 3 1292
闹比i
闹比i 2021-02-02 10:31

I want to start a program which needs several minutes to complete. During this time I want to read the progress message of the program (which are printed on the stdout). The pro

3条回答
  •  长情又很酷
    2021-02-02 11:09

    You can do a poll on the status of your subprocess and keep outputting lines.

    p = subprocess.Popen('ls;sleep 10', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    rc = p.poll()
    while rc != 0:
        while True:
            line = p.stdout.readline()
            if not line:
                break
            print line
        rc = p.poll()
    
    assert rc == 0
    

提交回复
热议问题