Flask-Restfu 淘流回应无效

2024-04-27 00:10:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个场景,我想通过flaskapi显示长时间运行脚本的输出。我引用了一个关于烧瓶的例子,它很管用。我在浏览器中得到dmesg蒸汽。在

import subprocess
import time
from flask import Flask, Response

app = Flask(__name__)

@app.route('/yield')
def index():
    def inner():
        proc = subprocess.Popen(
            ['dmesg'],  # call something with a lot of output so we can see it
            shell=True,
            stdout=subprocess.PIPE
        )

        for line in iter(proc.stdout.readline,''):
            time.sleep(1)  # Don't need this just shows the text streaming
            yield line.rstrip() + '<br/>\n'

    return Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show this

问题是,我已经使用Flask-Restful很久了。所以我想用它来做流媒体。我试过了,但没用。在

^{pr2}$

请帮忙


Tags: textimportappflaskfortimeresponsedef