使用Python和Flask流式传输数据会引发RuntimeError:在请求上下文之外工作

从问题:

Streaming data with Python and Flask

代码运行正常,我想修改函数g(),但是请求参数不能传递给g(),它引发了一个RuntimeError:在请求上下文之外工作.

我已经调试了很长时间,我不知道如何修改它,你能帮助查看代码并解释错误背后的原因吗?

谢谢.

我的代码是:

@app.route('/username',methods=['GET','POST'])
def index():
    req =request
    print req
    print "111------------"  + req.method + "\n"
    def ggg1(req):
        print req  # the req not my pass into the req....
        print "444------------" + req.method + "\n"
        if req.method == 'POST':
            if request.form['username']:
                urlList = request.form['username'].splitlines()
                i = 0
                for url in urlList():
                    i += 1
                    resultStr = chkListPageContent(url,getUrlContent(url),"script")
                    print i,resultStr
                    yield i,resultStr
    print req
    print "222------------" + req.method + "\n"
    return Response(stream_template('index.html',data=ggg1(req)))
最佳答案
您需要使用stream_with_context(). Reference

相关文章

我最近重新拾起了计算机视觉,借助Python的opencv还有face_r...
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Poolin...
记得大一学Python的时候,有一个题目是判断一个数是否是复数...
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic ...
文章目录 5 小提琴图Violinplot1. 基础小提琴图绘制 Basic v...
文章目录 4 核密度图Densityplot1. 基础核密度图绘制 Basic ...