Gevent has more compatibility issue than I thought... #2056
-
This is more of a general discussion regarding whether to use gevent with flask socketio in production. We are using gunicorn + gevent + flask socketio for our python backend service. As I use gevent with flask socketio more and more, I found that a lot of third party SDKs from GCP or Azure doesn't have good supports regarding gevent or eventlet due to its monkey patch nature. For references: A lot of the python SDK on the market doesn't support gevent really well and it creates high CPU usages and performance issues. Setting up flask socketio behind a nginx isn't exactly trivial effort either as documented here. I've also read a paper benchmarking websocket performance over multiple languages and frameworks and the conclusion was that python should be avoided at all costs when dealing with websocket connections. Not sure if that's still the case today. At this stage, neither migrating to python-socketio nor adopting a new language is a trivial task. Could someone give me some ideas and advise? Thank you so much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
With Flask-SocketIO you have three concurrency options:
Using threads is going to give you the biggest compatibility with third-party libraries libraries. Gevent and Eventlet provide higher scalability than threads, but the cost is that the asynchronous programming model is harder to set up, as you need to make sure that nothing in the application blocks for too long. The Eventlet project is currently on life support, so even though I used to like it more than Gevent at this point I cannot recommend it anymore.
I think this is a bit of an exaggeration. Google libraries have always had issues though, and Google never had interest in supporting Gevent. But many other libraries work well with Gevent's monkey patching of the standard library. As long as you stay away from CPU intensive tasks it shouldn't be too hard to get a working application.
What do you mean? Flask-SocketIO uses python-socketio. If you drop Flask-SocketIO and Flask and use python-socketio directly you will have access to asyncio concurrency, but this is another asynchronous platform, subject to most of the same issues Gevent and Eventlet have in terms of blocking and CPU use. I suggest you do a more careful analysis of your options with pyhton-socketio, I don't think you'll find better options unless you are interested in using asyncio for some specific reason.
Python is a slow language, and WebSocket has nothing to do with this. If you are obsessed with getting the best performance you should be using C/C++, Go, Rust, etc. WebSocket implementations in Python are not to blame, in my opinion.
I think you should try a multi-threaded web server and drop Gevent/Eventlet. |
Beta Was this translation helpful? Give feedback.
-
Hi @dongzeli95 since you are using gunicorn and gevent in production can you let me know who are you able to see access logs of gunicorn when using the geventwebsocket.gunicorn.workers.GeventWebSocketWorker worker because I'm not able to see the access log if I use this worker |
Beta Was this translation helpful? Give feedback.
With Flask-SocketIO you have three concurrency options:
async_mode='threading'
, and the--threads
option in Gunicorn)Using threads is going to give you the biggest compatibility with third-party libraries libraries. Gevent and Eventlet provide higher scalability than threads, but the cost is that the asynchronous programming model is harder to set up, as you need to make sure that nothing in the application blocks for too long. The Eventlet project is currently on life support, so even though I used to like it more than Gevent at this point I cannot recommend it anymore.
I think this is …