2013-11-03 11 views
5

Dalla domanda:dati in streaming con Python e Flask aumentare RuntimeError: al di fuori di lavoro della richiesta contesto

Streaming data with Python and Flask

Il codice è in esecuzione normale, e voglio modificare la funzione g(), ma il parametro di richiesta può non essere passato in g() e solleva un RuntimeError: working outside of request context.

Ho eseguito il debug per molto tempo e non so come modificarlo, puoi aiutarmi a rivedere il codice e spiegare il motivo dell'errore?

Grazie.

Il mio codice è:

@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))) 
+0

['copy_current_request_context()' non aiuta] (https://gist.github.com/6d2150f0eaf793320f6d) per ottenere un contesto di richiesta all'interno del generatore. Potresti provare a creare diversi generatori: uno per 'GET', un altro per' POST' che non usa 'request' e passa i dati necessari come parametri, ad esempio' if request.method == 'POST': def ggg_post (url_list) : yield "something" return Response (ggg_post (url_list = request.form ['username']. splitlines())) else: return ... ' – jfs

risposta

4

è necessario utilizzare stream_with_context(). Reference

+1

Mille upvotes :) – plaes

+0

È così utile! –

Problemi correlati