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()
A reliable way to read a stream without blocking regardless of operating system is to use
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
As information protection continues to be a growing concern for businesses today, IT security certifications have become highly desirable, even as the number of certifications has grown. Now you can s...
Cloud computing provides the capability to use computing and storage resources on a metered basis and reduce the investments in an organization’s computing infrastructure. The spawning and deletion ...
Acquire and analyze data from all corners of the social web with Python. This book is for intermediate Python developers who want to engage with the use of public APIs to collect data from social m...
In the summer of 2008, after three decades of building software in corporate data centers, I walked away from corporate America to take a stab at building a technology company from the ground up based...