Home > Backend Development > Python Tutorial > How Can I Perform Non-Blocking Reads from a Subprocess's Output Stream in Python?

How Can I Perform Non-Blocking Reads from a Subprocess's Output Stream in Python?

DDD
Release: 2024-12-17 22:06:12
Original
259 people have browsed it

How Can I Perform Non-Blocking Reads from a Subprocess's Output Stream in Python?

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
Copy after login

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()
Copy after login

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()
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template