Skip to content
Advertisement

Python: How to read stdout of subprocess in a nonblocking way

I am trying to make a simple python script that starts a subprocess and monitors its standard output. Here is a snippet from the code:

JavaScript

The problem is that the script hangs on output=process.stdout.readline() and that the line print "test" only executes after the subprocess is terminated.

Is there a way to read standard output and print it without having to wait for the subprocess to terminate?

The subprocess which I am starting is a Windows binary for which I do not have the source code.

I have found several similar questions, but the answers are only applicable on Linux or in case I have the source of the suprocess I am starting.

Advertisement

Answer

Check select module

JavaScript

EDIT:

Threaded Solution for non posix systems:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement