Search code examples
pythongunicorngeventflask-socketiogevent-socketio

How many websocket clients can flask-socketio handle when in Gunicorn with gevent?


I am considering the following setup:

If you're really curious, here's the source tying it all together.

This seems to work, and be able to serve both normal HTTP traffic to the flask app as well as websockets to the socketio app. My question is: how? And how scalable is it (particularly regarding the websocket piece)?

My understanding of WSGI is that each request that comes in (http or websocket) will block a worker until the connection is terminated. This would mean that this setup would only be able to handle one websocket connection at a time. I've done enough experimenting to not think that's the case, but I still don't know how it works. Are the websockets being handled outside Gunicorn? If so, why do you have to give Gunicorn the GeventWebSocketWorker class? If they are being handled from Gunicorn, how is that possible when Gunicorn is WSGI compliant and WSGI doesn't seem to really allow more than one connection per worker?

On top of these theoretical questions, I also want to know how many websocket connections I can handle with this setup (ballpark).


Solution

  • My understanding of WSGI is that each request that comes in (http or websocket) will block a worker until the connection is terminated. This would mean that this setup would only be able to handle one websocket connection at a time.

    This is correct if you use Gunicorn's "sync" worker. If you use one of the asynchronous workers (gevent or eventlet) you can get thousands of connections with a single worker. You are using the gevent worker, so that's what enables concurrency in your case.

    I also want to know how many websocket connections I can handle with this setup (ballpark).

    Flask-SocketIO does not have a limit. Gunicorn I believe uses 1000 connections per worker by default, but this can be changed via configuration. Other limits might affect your connection count as well, such as number of file handles that can be allocated from the operating system.

    Also note that you can scale your server horizontally and get more connections that way.