python-连续读取linux命令输出
我有一个命令,每隔几秒钟提供一次事件流-新消息。 我如何理解python附带的内容 标准方法python-连续读取linux命令输出,python,python-3.x,Python,Python 3.x,我有一个命令,每隔几秒钟提供一次事件流-新消息。 我如何理解python附带的内容 标准方法 def getMessage(command): lines = os.popen(command).readlines() return lines 等待命令完成,但在此命令中将永远运行。它将继续并每隔几秒钟将新消息打印到stdout。 如何将其传递给python?我想捕获流中的所有消息。您可以逐行读取输出并处理/打印它。同时使用p.poll检查流程是否已结束 def getMess
def getMessage(command):
lines = os.popen(command).readlines()
return lines
等待命令完成,但在此命令中将永远运行。它将继续并每隔几秒钟将新消息打印到stdout。
如何将其传递给python?我想捕获流中的所有消息。您可以逐行读取输出并处理/打印它。同时使用
p.poll
检查流程是否已结束
def getMessage(command):
full_message = ""
p = subprocess.Popen(command, stdout=subprocess.PIPE)
while True:
output = p.stdout.readline()
if output == '' and p.poll() is not None:
break
if output:
fullmessage += output
print(output.strip())
return full_message
我需要做一些小的修改:
p=subprocess.Popen(command,stdout=subprocess.PIPE,shell=True,universal\u newlines=True)
,但除此之外它工作得很好,谢谢!