How to get output from subprocess.Popen(). proc.stdout.readline() blocks, no data prints out

后端 未结 4 1750
自闭症患者
自闭症患者 2020-11-28 05:32

I want output from execute Test_Pipe.py, I tried following code on Linux but it did not work.

Test_Pipe.py

import time
while True :         


        
4条回答
  •  粉色の甜心
    2020-11-28 06:17

    Nadia's snippet does work but calling read with a 1 byte buffer is highly unrecommended. The better way to do this would be to set the stdout file descriptor to nonblocking using fcntl

    fcntl.fcntl(
        proc.stdout.fileno(),
        fcntl.F_SETFL,
        fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK,
    )
    

    and then using select to test if the data is ready

    while proc.poll() == None:
        readx = select.select([proc.stdout.fileno()], [], [])[0]
        if readx:
            chunk = proc.stdout.read()
            print chunk
    

    She was correct in that your problem must be different from what you posted as Caller.py and Test_Pipe.py do work as provided.

    • https://derrickpetzold.com/p/capturing-output-from-ffmpeg-python/

提交回复
热议问题