Stop Reading Process Output in Python Without Hangs
When using os.popen() to capture process output, the program can hang if the process continuously outputs data. To address this issue, consider using alternative methods such as:
Using Subprocess and Threads
Start the process with subprocess.Popen, create a thread to read the output, and store it in a queue. Terminate the process after a specific timeout.
<code class="python">import subprocess import threading import time def read_output(process, append): for line in iter(process.stdout.readline, ""): append(line) def main(): process = subprocess.Popen(["top"], stdout=subprocess.PIPE, close_fds=True) try: q = collections.deque(maxlen=200) t = threading.Thread(target=read_output, args=(process, q.append)) t.daemon = True t.start() time.sleep(2) finally: process.terminate() print(''.join(q))</code>
Using Signal Handler
Use signal.alarm() to raise an exception after a specified timeout. This forces the process to terminate, allowing the output to be captured.
<code class="python">import signal import subprocess import time def alarm_handler(signum, frame): raise Alarm def main(): process = subprocess.Popen(["top"], stdout=subprocess.PIPE, close_fds=True) signal.signal(signal.SIGALRM, alarm_handler) signal.alarm(2) q = collections.deque(maxlen=200) try: for line in iter(process.stdout.readline, ""): q.append(line) signal.alarm(0) except Alarm: process.terminate() finally: print(''.join(q))</code>
Using Timer
Employ threading.Timer to terminate the process after a timeout.
<code class="python">import threading import subprocess def main(): process = subprocess.Popen(["top"], stdout=subprocess.PIPE, close_fds=True) timer = threading.Timer(2, process.terminate) timer.start() q = collections.deque(process.stdout, maxlen=200) timer.cancel() print(''.join(q))</code>
Alternative Approaches
Note that these solutions may introduce additional complexity or compatibility issues. Choose the approach that best suits your specific needs and platform.
The above is the detailed content of How Can I Prevent Python Programs from Hanging When Capturing Continuous Process Output?. For more information, please follow other related articles on the PHP Chinese website!