pythonnginxflaskstreaminggunicorn

Streaming server issue with gunicorn and flask and Nginx


I am using gunicorn and flask for a web service. I am trying to get my head around running a streaming route (not sure if that is the correct terminology).

my route looks like this:

@app.route('/delay')
def delay():
    from time import sleep
    def delay_inner():
        for i in range(10):
            sleep(5)
            yield json.dumps({'delay': i})
    return Response(delay_inner(), mimetype="text/event-stream")

I expect that the server would yield the output each time that delay_inner does a yield. But, what I am getting is all the json responses at once, and only when the delay_inner finishes execution.

What am I missing here?

--EDIT-- I have fixed the issue for Flask and Gunicorn, I am able to run it as expected by using the flask server, and by going to the Gunicorn port. It streams the data as expected. However, and I should have mentioned this in the original post, I am also running behind nginx. And that is not set up correctly to stream. Can anyone help with that?


Solution

  • You need to turn off the nginx proxy buffering.

    location /delay {
             proxy_pass http://127.0.0.1:8080;
             proxy_buffering off;
    }
    

    and reload the config

    nginx -s reload