Non-Blocking Reads on Subprocess Output Streams in Python
In Python, the subprocess module provides a convenient way to launch and interact with external processes. However, it's crucial to handle I/O interactions between the parent and child processes efficiently.
Consider the following scenario: you wish to perform non-blocking reads on the standard output stream of a subprocess. By default, p.stdout.readline() blocks until data becomes available in the buffer. To address this, we can employ a technique that involves a separate thread and a queue.
First, create a queue to store output lines:
from Queue import Queue
Next, start a thread that reads lines from the subprocess stdout and enqueues them:
def enqueue_output(out, queue): for line in iter(out.readline, b''): queue.put(line) out.close()
Initialize the subprocess and start the thread:
p = Popen(['myprogram.exe'], stdout=PIPE) q = Queue() t = Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start()
To read a line non-blocking, use the following:
try: line = q.get_nowait() # or q.get(timeout=.1) except Empty: print('no output yet') else: # got line # do something with the line
This method is cross-platform compatible and ensures non-blocking reads, allowing you to efficiently process data from the subprocess.
The above is the detailed content of How Can I Perform Non-Blocking Reads from a Subprocess's Output Stream in Python?. For more information, please follow other related articles on the PHP Chinese website!