gpt4 book ai didi

Python:启动终端程序并在运行时解析其输出

转载 作者:太空宇宙 更新时间:2023-11-03 18:10:49 24 4
gpt4 key购买 nike

我正在为流行的 NodeJS 服务器“MeteorJS”编写 Ubuntu 的 AppIndicator,它应该列出可用的项目并可以启动服务器,当它启动服务器时,它会获取其终端输出并对它们使用react。

当您启动 meteor 时,它会根据发生的情况提供一些输出。例如,当您更改代码时,它会输出 "changed restarting..." 或者当您再次更改 "changed restarting... (2x)" 就可以了,但是当它有错误时,它会打印一些错误消息。

这没问题,除非您的桌面上没有足够的空间来查看该终端。

所以我编写了一个应用程序,应该以另一种方式通知我这些消息。

我的实际问题:

我需要从 python 程序启动服务器,同时我可以对服务器在其 stdout 上写入的输出准确地出现时使用react。

所以我想

  • 打开终端程序
  • 将其发送到后台,以便我可以完成我的工作
  • 对打印的每一行使用react

最佳答案

您可能想要研究线程化以下函数,然后弄清楚如何使其成为“事件驱动”。

但这就是我在后台运行 bash 脚本并在我感兴趣时获取其输出的方式。

# To test it out, run the script and then turn your wifi off and on.

import subprocess


def tail():
command = ["tail", "-f", "/var/log/wifi.log"] # your bash script execute command goes here
popen = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in iter(popen.stdout.readline, ""):
yield line,

logger = tail()

for line in logger:
print line

关于Python:启动终端程序并在运行时解析其输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26006744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com