Penerangan Masalah:
Dalam atur cara Python menggunakan os.popen () atau subprocess.Popen() untuk membaca output proses pengemaskinian yang berterusan (seperti atas), program mungkin tergantung apabila cuba membaca semua baris menggunakan readlines().
Menggunakan Fail Sementara dan Proses Anak:
<code class="python">import subprocess import tempfile import time def main(): # Open a temporary file for process output with tempfile.TemporaryFile() as f: # Start the process and redirect its stdout to the file process = subprocess.Popen(["top"], stdout=f) # Wait for a specified amount of time time.sleep(2) # Kill the process process.terminate() process.wait() # Wait for the process to terminate to ensure complete output # Seek to the beginning of the file and print its contents f.seek(0) print(f.read()) if __name__ == "__main__": main()</code>
Pendekatan ini menggunakan fail sementara untuk menyimpan output proses, membenarkan atur cara mengelak daripada menyekat pada readlines().
Menggunakan Baris Gilir dengan Benang Lain:
<code class="python">import collections import subprocess import threading def main(): # Create a queue to store process output q = collections.deque() # Start the process and redirect its stdout to a thread process = subprocess.Popen(["top"], stdout=subprocess.PIPE) t = threading.Thread(target=process.stdout.readline, args=(q.append,)) t.daemon = True t.start() # Wait for a specified amount of time time.sleep(2) # Terminate the process process.terminate() t.join() # Wait for the thread to finish # Print the stored output print(''.join(q)) if __name__ == "__main__": main()</code>
Menggunakan signal.alarm():
<code class="python">import collections import signal import subprocess class Alarm(Exception): pass def alarm_handler(signum, frame): raise Alarm def main(): # Create a queue to store process output q = collections.deque() # Register a signal handler to handle alarm signal.signal(signal.SIGALRM, alarm_handler) # Start the process and redirect its stdout process = subprocess.Popen(["top"], stdout=subprocess.PIPE) # Set an alarm to terminate the process after a specified amount of time signal.alarm(2) # Read lines until the alarm is raised or the process terminates try: while True: line = process.stdout.readline() if not line: break q.append(line) except Alarm: process.terminate() # Cancel the alarm if it hasn't already fired signal.alarm(0) # Wait for the process to finish process.wait() # Print the stored output print(''.join(q)) if __name__ == "__main__": main()</code>
Alternatif ini membenarkan program untuk terus berjalan sambil menyimpan output proses. Ia mungkin lebih sesuai untuk kes di mana anda perlu memantau output proses secara berterusan.
Atas ialah kandungan terperinci Bagaimana untuk Mengelakkan Readline Hang Apabila Menghentikan Output Proses dalam Python?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!