我正在使用subprocess 模块来启动一个子进程并连接到它的输出流(标准输出)。我希望能够在其标准输出上执行非阻塞读取。有没有办法使 .readline 非阻塞或在我调用之前检查流上是否有数据.readline?我希望它是可移植的,或者至少可以在 Windows 和 Linux 下工作。
.readline
这是我现在的做法(.readline如果没有可用数据,它会阻止):
p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE) output_str = p.stdout.readline()
fcntl,select,asyncproc`在这种情况下无济于事。
fcntl,
,
无论操作系统如何,在不阻塞的情况下读取流的可靠方法是使用Queue.get_nowait():
Queue.get_nowait()
import sys from subprocess import PIPE, Popen from threading import Thread try: from queue import Queue, Empty except ImportError: from Queue import Queue, Empty # python 2.x ON_POSIX = 'posix' in sys.builtin_module_names def enqueue_output(out, queue): for line in iter(out.readline, b''): queue.put(line) out.close() p = Popen(['myprogram.exe'], stdout=PIPE, bufsize=1, close_fds=ON_POSIX) q = Queue() t = Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True # thread dies with the program t.start() # ... do other things here # read line without blocking try: line = q.get_nowait() # or q.get(timeout=.1) except Empty: print('no output yet') else: # got line # ... do something with line