Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming data with Python and Flask raise RuntimeError: working outside of request context

From the question:

Streaming data with Python and Flask

The code is running normal, and I want to modify the function g(), but the request parameter can not be passed into g() and it raises a RuntimeError: working outside of request context.

I have been debugging for a long time, and I don't know how to modify it, can you help review the code and explain the reason behind the error?

Thanks.

My code is:

@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)))
like image 768
june Avatar asked Nov 03 '13 17:11

june


1 Answers

You need to use stream_with_context(). Reference

like image 165
akg Avatar answered Oct 29 '22 18:10

akg