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

2024-09-23 | code | python, io, subprocess, nonblocking

问题描述

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):

提示词

None
Loading...