


How to Avoid Python Programs Hanging When Reading Continuous Process Output?
Stopping Process Output Reading in Python Without Hangs
Background
When using Python's os.popen() function with tools that produce continuous output, the program often hangs when trying to read the output.
os.popen() Issue
The problematic line process = os.popen("top").readlines() halts the program due to readlines(), which attempts to read the entire process output at once.
Solution with subprocess.Popen()
To resolve this issue, use subprocess.Popen() instead of os.popen(). Here's a corrected example:
<code class="python">import subprocess import time import os # Start "top" process with stdout redirection process = subprocess.Popen(["top"], stdout=subprocess.PIPE) # Wait for 2 seconds time.sleep(2) # Send kill signal to "top" process os.popen("killall top") # Read process output output, _ = process.communicate() print(output.decode())</code>
This modified code:
- Captures the process output in a variable using communicate() instead of readlines().
- Sends the kill signal to the "top" process.
- Declares end-of-file for the process's I/O stream and exits the program.
Tail-like Approach
If you only need a portion of the process output, you can use a tail-like solution to capture a specific number of lines.
Thread-based Approach
To capture process output in a separate thread, try the following:
<code class="python">import collections import subprocess import threading # Start process with stdout redirection process = subprocess.Popen(["top"], stdout=subprocess.PIPE) # Define function to read process output in a thread def read_output(process): for line in iter(process.stdout.readline, ""): ... # Implement your logic here to process each line # Create and start a thread for reading and processing output reading_thread = threading.Thread(target=read_output, args=(process,)) reading_thread.start() # Wait for 2 seconds, then terminate the process time.sleep(2) process.terminate() # Wait for the reading thread to complete reading_thread.join()</code>
signal.alarm() Approach
You can also use signal.alarm() to terminate the process after a specified timeout:
<code class="python">import collections import signal import subprocess # Define signal handler def alarm_handler(signum, frame): # Raise an exception to terminate the process reading raise Exception # Set signal handler and alarm for 2 seconds signal.signal(signal.SIGALRM, alarm_handler) signal.alarm(2) # Start process with stdout redirection process = subprocess.Popen(["top"], stdout=subprocess.PIPE) # Capture process output number_of_lines = 200 q = collections.deque(maxlen=number_of_lines) for line in iter(process.stdout.readline, ""): q.append(line) # Cancel alarm signal.alarm(0) # Print captured output print(''.join(q))</code>
threading.Timer Approach
Alternatively, you can use threading.Timer to schedule the process termination:
<code class="python">import collections import subprocess import threading # Define function to terminate the process def terminate_process(process): process.terminate() # Start process with stdout redirection process = subprocess.Popen(["top"], stdout=subprocess.PIPE) # Create and start a timer to terminate process in 2 seconds timer = threading.Timer(2, terminate_process, [process]) timer.start() # Capture process output number_of_lines = 200 q = collections.deque(process.stdout, maxlen=number_of_lines) # Cancel timer timer.cancel() # Print captured output print(''.join(q))</code>
The above is the detailed content of How to Avoid Python Programs Hanging When Reading Continuous Process Output?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
