Is there a way to poll a file handle returned from subprocess.Popen?

后端 未结 2 429
一生所求
一生所求 2021-02-04 20:46

Say I write this:

from subprocessing import Popen, STDOUT, PIPE
p = Popen([\"myproc\"], stderr=STDOUT, stdout=PIPE)

Now if I do



        
2条回答
  •  梦毁少年i
    2021-02-04 21:45

    Use p.stdout.read(1) this will read character by character

    And here is a full example:

    import subprocess
    import sys
    
    process = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    
    while True:
        out = process.stdout.read(1)
        if out == '' and process.poll() != None:
            break
        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()
    

提交回复
热议问题