Retrieving Realtime Output with Subprocess
To obtain real-time output from a command-line program using subprocess, the p.stdout.readline() method can be employed. This method differs from p.stdout in terms of buffering behavior. While p.stdout buffers output aggressively, p.stdout.readline() reads each line as it becomes available.
The following Python code demonstrates the use of p.stdout.readline():
from subprocess import Popen, PIPE, STDOUT p = Popen('svnadmin verify /var/svn/repos/config', stdout=PIPE, stderr=STDOUT, shell=True) while True: line = p.stdout.readline() if not line: break print(line.replace('\n', ''))
This code reads each line of output from the svnadmin verify command and prints it in real time.
The above is the detailed content of How can I Retrieve Realtime Output from a Command-line Program Using `subprocess` in Python?. For more information, please follow other related articles on the PHP Chinese website!