我正在使用子进程模块启动子进程并连接到它的输出流(stdout).我希望能够在其标准输出上执行非阻塞读取.有没有办法让.readline非阻塞或在我调用之前检查流上是否有数据.readline?我希望这是可移植的,或至少在Windows和Linux下工作.
这是我现在如何做到的(.readline如果没有数据可用,则阻止它):
p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
output_str = p.stdout.readline()
Run Code Online (Sandbox Code Playgroud) 需要:
Python 3子进程模块内置了超时,我也尝试使用计时器和使用线程实现超时,但它不能与输出一起使用.readline()阻止与否?readlines()肯定会在吐出所有输出之前等待进程结束,这不是我需要的(我需要继续).
我接近切换到node.js :-(