[Python] A non-blocking read on a subprocess.PIPE in Python

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the subprocess module to start a subprocess and connect to its output stream (standard output). I want to be able to execute non-blocking reads on its standard output. Is there a way to make .readline non-blocking or to check if there is data on the stream before I invoke .readline? I'd like this to be portable or at least work under Windows and Linux.

    Here is how I do it for now (it's blocking on the .readline if no data is available):

    p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
    output_str = p.stdout.readline()

    Continue reading...

Compartilhe esta Página