HTTP Streaming with Apache mod_wsgi

2019-08-04 00:23发布

问题:

I've got an ubuntu-server where I am running multiple web-apps. All of them are hosted by Apache using named VirtualHosts. One of them is a Flask app, which is running via mod_wsgi. This app is serving continuous, unlimited HTTP streams.

Does this eventually block my app/server/apache worker, if enough clients are connecting to the streaming endpoint? And if yes, are there alternatives? Other non-blocking wsgi-servers that play nicely with VirtualHosts, a different http-streaming paradigm, or some magic apache mod_wsgi settings?

The core of it looks like:

@app.route('/stream')
def get_stream():
    def endless():
        while True:
            yield get_stuff_from_redis()
            time.sleep(1)

    return Response(endless(), mimetype='application/json')

回答1:

If the clients never disconnect, yes, you will eventually run out of processes/threads to handle more requests.

You are more than likely better off using a async framework such as Tornado or Twisted for this specific type of application. Doing async programming can be tricky if you aren't used to that concept.

Some people use coroutine system such as gevent/eventlet, but they also have their own problems you have to watch out for.