0
votes

I have read about Python websockets Library and it actually worked for me. I was especially happy with standard way of connecting from web client:

var ws = new WebSocket("ws://127.0.0.1:5678/")
ws.onmessage = function (event) ...

No imports, everything is standard and supported by modern browsers out of the box. So far so good, but Python application is Flask with Connexion for swagger.yaml-based REST API. Websockets library cannot share the same port with Flask/Connexion. OK, there is Flask-socketio library which I was able to run with Connexion and server both REST and Websockets. But for me it has several drawbacks, and I'm asking for your help:

  1. On client, I'm forced to use socketio, as browser-supported WebSocket won't work with Flask-socketio. It is always the case, or am I doing something wrong (this is linked with question 2).
  2. So far I had success running Flask-socketio under Flask debug mode (python my_flask_source.py). I have read Flask-socketio documentation and understand that this mode does not support Websockets, only long polling. Indeed, I see many GET/POST requests coming in every few seconds and served by Flask. Customer is using uWSGI for production, however it is not very clear for me where to start to reproduce their config in development. I'm quite new to Python, but by reading as many relevant sources as I could find I understood that Flask development server is multithreaded, meaning it can serve many parallel requests in single process. But it looks like uWSGI is single-threaded and one has to spawn many processes in order to achieve high throughput. Also, there is some gevent module:

uwsgi --http :5000 --gevent 1000 --http-websockets --master --wsgi-file app.py --callable app

Is there any well-written step by step instruction of deploying Flask-socketio server under uWSGI for production? Does nginx come into play? Will I be able to use Websockets in browser, or still socketio?

1
I don't think flask's development server is multi-threaded - C.Nivs
I have written a GET method which will output current time, sleep for 10 seconds, output current time again and exit. Called it from two browser windows and observed the log - second GET did not wait for the completion of the first one, instead it started immediately after receiving request. - DimaA6_ABC
In the old days, Flask's dev server was single threaded by default, and could be configured as multithreaded with an option. Current version of Flask runs multithreaded by default. - Miguel

1 Answers

2
votes

Let me try to clarify:

  1. If you use a Socket.IO server you must use a Socket.IO client. Socket.IO is not the same as WebSocket.

  2. You are running uWSGI with gevent, so concurrency is handled via greenlets. The --gevent 1000 argument configures 1000 concurrent tasks. You do not need to spawn multiple uWSGI processes unless you ran out of those 1000.

Is there any well-written step by step instruction of deploying Flask-socketio server under uWSGI for production?

All I can offer is the Flask-SocketIO and python-socketio documentation.

Does nginx come into play?

Up to you, really. The uWSGI server is production-ready, but it is fairly common to put it behind nginx and have nginx terminate TLS, serve static content, etc.

Will I be able to use Websockets in browser, or still socketio?

Nope. See above.