当将 Python 的 os.popen() 函数与产生连续输出的工具一起使用时,尝试读取输出时程序经常挂起。
有问题的行进程 = os.popen("top").readlines() 由于以下原因暂停程序readlines(),它尝试一次读取整个进程的输出。
要解决此问题,请使用 subprocess.Popen() 而不是 os.popen ()。这是一个更正的示例:
<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>
此修改后的代码:
如果只需要进程输出的一部分,可以使用类似尾部的解决方案来捕获特定数量的行。
捕获进程在单独的线程中输出,请尝试以下操作:
<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() 在指定超时后终止进程:
<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 来安排进程终止:
<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>
以上是如何避免Python程序在读取连续进程输出时挂起?的详细内容。更多信息请关注PHP中文网其他相关文章!