How to use flask-socketIo with a threaded flask application #1601
Replies: 15 comments
-
Perhaps #1473 answers your question? |
Beta Was this translation helpful? Give feedback.
-
Thank you for the help but I have already seen this topic and it does not really meet my expectations ... I want to run my application in a web server like eventlet for example but supporting threading (several requests at the same time ) ... |
Beta Was this translation helpful? Give feedback.
-
@MathiasDrapier I think you need to read up on how eventlet and other async frameworks achieve concurrency.
Eventlet does not support threads. It supports greenlets instead.
It can do this easily, and actually can scale much better than threads, as long as you don't block.
I did not make the rules. That's how eventlet works. If you don't like it, then you have to use regular threads and pay a performance penalty. I'm working on support for WebSocket in threading mode, so this is going to be available in the near future, but right now you have no other choices, so you need to decide which option you hate less. Sorry. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response @miguelgrinberg . Is it possible with Eventlet to answer several requests at the same time? If so, how to do it? |
Beta Was this translation helpful? Give feedback.
-
@MathiasDrapier I mentioned it above. You are likely running blocking code in your handlers. Once you stop doing that, concurrency will work. |
Beta Was this translation helpful? Give feedback.
-
What do you mean as the blocking code ? |
Beta Was this translation helpful? Give feedback.
-
@MathiasDrapier you are trying to work with something you are not familiar with. I suggested above that if you intend to use eventlet you need to familiarize yourself with greenlets and how they achieve concurrency. You really need to understand what code is blocking under eventlet and how to avoid it. And if you don't want to make an investment in learning yet another thing, then go back to the threading mode and things will work better, just a bit slower and without WebSocket for now. |
Beta Was this translation helpful? Give feedback.
-
Ok thank you, but with threading mode my socketio client is disconnected a few seconds after its connection. |
Beta Was this translation helpful? Give feedback.
-
I need to see logs of that. See the troubleshooting section of the docs if you don't know how to get them. |
Beta Was this translation helpful? Give feedback.
-
Here is my logs :
|
Beta Was this translation helpful? Give feedback.
-
Your client is closing the connection:
|
Beta Was this translation helpful? Give feedback.
-
Okay sorry, I will check it. And concerning the use of eventlet, I will wait and document myself on this subject. Thank you very much for the relevant and quick response. |
Beta Was this translation helpful? Give feedback.
-
What is the transport used if it is not the websocket? I must specify it on the client side to allow it to connect. |
Beta Was this translation helpful? Give feedback.
-
Polling. This is the default, no need to specify it. |
Beta Was this translation helpful? Give feedback.
-
Okay thanks |
Beta Was this translation helpful? Give feedback.
-
Hello, I am using flask-socketio to add socketio functionality in my flask api.
Until now everything worked fine by initializing the application like this:
main.py :
socketio.run(app, host='::', port=1234)
app.py:
socketio = SocketIO(app, always_connect=True)
But I noticed that my application could not answer several requests at the same time.
So I initialized socketIo like this:
socketio = SocketIO(app, always_connect=True, async_mode="threading")
This time my API is able to respond to several requests at the same time but when I start my application I receive the following log:
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
And my Socketio client gets disconnected 5-6 seconds after logging in.
I searched here and couldn't find a solution except removing the asyn_mode = threading and putting sleeps in the long requests but I don't want to do this. My application should not be able to manage a lot of customers so even quick requests should be able to be requested at the same time.
Beta Was this translation helpful? Give feedback.
All reactions