我正在尝试将dameon服务的状态输送到websocket中。它可以工作,但它只显示在运行代码时日志中的信息。它不会等待并向我展示新的信息,比如我运行其他进程时。
我使用flask-sock作为websocket,我使用subprocess.popen将信息管道到websocket。
@sock.route('/eprotectview')
def eprotectview(ws):
data = ws.receive()
with subprocess.Popen(['systemctl status eprotection'], stdout=subprocess.PIPE, shell=True, bufsize=1,
universal_newlines=True) as process:
for line in process.stdout:
line = line.rstrip()
print(line)
try:
ws.send(line + "\n")
except:
pass它工作,但它没有显示所有的线条,来晚些时候。
有没有办法让systemctl在所有数据出现时显示出来?或者有其他程序可以给我实时状态的dameon服务?在这里输入图像描述
发布于 2022-02-18 22:29:03
我想出来了!(但如果你知道更好的方法,请张贴)。
我使用了journalctl -u eprotection -f,这给了我实时的信息,所以如果出现新的事件,它将显示在websocket中:
@sock.route('/eprotectview')
def eprotectview(ws):
data = ws.receive()
with subprocess.Popen(['journalctl -u eprotection -f'],stdout=subprocess.PIPE,shell=True,bufsize=1,universal_newlines=True) as process:
for line in process.stdout:
line = line.rstrip()
print(line)
try:
ws.send(line+ "\n")
except:
passhttps://stackoverflow.com/questions/71180386
复制相似问题