This question already has an answer here:
这个问题在这里已有答案:
- Display data streamed from a Flask view as it updates 1 answer
- 显示从Flask视图流式传输的数据,因为它更新了1个答案
Here is my Flask code. While it is not properly implemented and not working, it at least gives you an idea of what I am trying to do.
这是我的Flask代码。虽然没有正确实施并且无法正常工作,但它至少可以让您了解我想要做什么。
@app.route('/runid', methods=['GET','POST'])
def execCmdLocal(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True )
stdout, stderr = proc.communicate()
status = proc.poll()
if status != 0:
print("Failed to execute command %s" % cmd)
return status, stdout, stderr
def runid()
time.sleep(1)
cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
ret = execCmdLocal(cmdStr)
logName = ret[1].strip()
print('The DartRunner Log generated is: %s'%logName)
with open('/root/Dart/log/' + logName, "r") as fd:
for line in fd:
if 'runId' in line:
runId = line.split()[-1]
print('Run Id: %s'%runId)
break
print runID
return runId
My original Python script is:
我原来的Python脚本是:
import os
from pprint import pprint
import time
def execCmdLocal(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True )
stdout, stderr = proc.communicate()
status = proc.poll()
if status != 0:
print("Failed to execute command %s" % cmd)
return status, stdout, stderr
time.sleep(1)
cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
ret = execCmdLocal(cmdStr)
#pprint(ret)
logName = ret[1].strip()
print('The DartRunner Log generated is: %s'%logName)
with open(logName, "r") as fd:
for line in fd:
if 'runId' in line:
runId = line.split()[-1]
print('Run Id: %s'%runId)
break;
The output I have is:
我的输出是:
The DartRunner Log generated is: DartRunner-2018-06-07-145652.log Run Id: 180607-133
生成的DartRunner日志是:DartRunner-2018-06-07-145652.log运行ID:180607-133
This function is printed when the whole program is started. But I am trying to get it only when I call my restAPI route.
整个程序启动时打印此功能。但是我只是在打电话给我的restAPI路线时试图才能得到它。
Thanks in advance.
提前致谢。
1 个解决方案
#1
0
The code changes should be like this. By default Flask request is GET, I hope you just need to execute a process. Also, try to return a JSON_RESPONSE for a flask request. Try to learn Flask basics for a better understanding.
代码更改应该是这样的。默认Flask请求是GET,我希望你只需要执行一个进程。另外,尝试为烧瓶请求返回JSON_RESPONSE。尝试学习Flask基础知识以便更好地理解。
@app.route('/runid')
def runid():
cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
ret = execCmdLocal(cmdStr)
logName = ret[1].strip()
print('The DartRunner Log generated is: %s'%logName)
with open('/root/Dart/log/' + logName, "r") as fd:
for line in fd:
if 'runId' in line:
runId = line.split()[-1]
print('Run Id: %s'%runId)
break
return jsonify(runId)
def execCmdLocal(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True )
stdout, stderr = proc.communicate()
status = proc.poll()
if status != 0:
print("Failed to execute command %s" % cmd)
return status, stdout, stderr
#1
0
The code changes should be like this. By default Flask request is GET, I hope you just need to execute a process. Also, try to return a JSON_RESPONSE for a flask request. Try to learn Flask basics for a better understanding.
代码更改应该是这样的。默认Flask请求是GET,我希望你只需要执行一个进程。另外,尝试为烧瓶请求返回JSON_RESPONSE。尝试学习Flask基础知识以便更好地理解。
@app.route('/runid')
def runid():
cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
ret = execCmdLocal(cmdStr)
logName = ret[1].strip()
print('The DartRunner Log generated is: %s'%logName)
with open('/root/Dart/log/' + logName, "r") as fd:
for line in fd:
if 'runId' in line:
runId = line.split()[-1]
print('Run Id: %s'%runId)
break
return jsonify(runId)
def execCmdLocal(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True )
stdout, stderr = proc.communicate()
status = proc.poll()
if status != 0:
print("Failed to execute command %s" % cmd)
return status, stdout, stderr