Apabila menggunakan fungsi os.popen() Python dengan alatan yang menghasilkan output berterusan, program sering digantung apabila cuba membaca output.
Proses talian bermasalah = os.popen("top").readlines() menghentikan program kerana readlines(), yang cuba membaca keseluruhan output proses sekaligus.
Untuk menyelesaikan isu ini, gunakan subprocess.Popen() dan bukannya os.popen (). Berikut ialah contoh yang diperbetulkan:
<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>
Kod yang diubah suai ini:
Jika anda hanya memerlukan sebahagian daripada output proses, anda boleh menggunakan penyelesaian seperti ekor untuk menangkap bilangan baris tertentu.
Untuk menangkap proses output dalam urutan berasingan, cuba yang berikut:
<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>
Anda juga boleh menggunakan signal.alarm() untuk menamatkan proses selepas tamat masa yang ditentukan:
<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>
Sebagai alternatif, anda boleh menggunakan penjalinan.Pemasa untuk menjadualkan penamatan proses:
<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>
Atas ialah kandungan terperinci Bagaimana untuk Mengelakkan Program Python Tergantung Semasa Membaca Output Proses Berterusan?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!