Python Websockets in Flask

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23111654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:16:00  来源:igfitidea点击:

Websockets in Flask

pythonwebsocketflaskuwsgigevent

提问by ruipacheco

I'm currently researching websocket support in Python and am a bit confused with the offerings.

I'm currently researching websocket support in Python and am a bit confused with the offerings.

On one hand it's possible to use Flask + gevent. On the other hand, uwsgi has socket supportand at last there is an extension that bundles both uwsgi and gevent.

On one hand it's possible to use Flask + gevent. On the other hand, uwsgi has socket supportand at last there is an extension that bundles both uwsgi and gevent.

What's the problem with implementing websockets with only one of these? What do I win by mixing them?

What's the problem with implementing websockets with only one of these? What do I win by mixing them?

Changing the question

Changing the question

What does adding gevent do that threaded uwsgi won't?

What does adding gevent do that threaded uwsgi won't?

采纳答案by Miguel

In regular HTTP requests the connections between client and server are short-lived, a client connects to the server, sends a request, receives the response and then closes the connection. In this model the server can serve a large number of clients using a small number of workers. The concurrency model in this situation is typically based on threads, processes or a combination of both.

In regular HTTP requests the connections between client and server are short-lived, a client connects to the server, sends a request, receives the response and then closes the connection. In this model the server can serve a large number of clients using a small number of workers. The concurrency model in this situation is typically based on threads, processes or a combination of both.

When you use websocket the problem is more complex, because a websocket connection is open for a long period of time, so the server cannot use a small pool of workers to serve a large number of clients, each client needs to get its own dedicated worker. If you use threads and/or processes then your app will not scale to support a large number of clients because you can't have large number of threads/processes.

When you use websocket the problem is more complex, because a websocket connection is open for a long period of time, so the server cannot use a small pool of workers to serve a large number of clients, each client needs to get its own dedicated worker. If you use threads and/or processes then your app will not scale to support a large number of clients because you can't have large number of threads/processes.

This is where gevent enters the picture. Gevent has a concurrency model based on greenlets, which scale much better than threads/processes. So serving websocket connections with a gevent based server allows you support more clients, due to the lightweight nature of greenlets. With uWSGI you have a choice of concurrency models to use with web sockets, and that includes the greenlet based model from gevent. You can also use gevent's web server standalone if you want.

This is where gevent enters the picture. Gevent has a concurrency model based on greenlets, which scale much better than threads/processes. So serving websocket connections with a gevent based server allows you support more clients, due to the lightweight nature of greenlets. With uWSGI you have a choice of concurrency models to use with web sockets, and that includes the greenlet based model from gevent. You can also use gevent's web server standalone if you want.

But note that gevent does not know anything about web sockets, it is just a server. To use websocket connections you have to add an implementation of the websocket server.

But note that gevent does not know anything about web sockets, it is just a server. To use websocket connections you have to add an implementation of the websocket server.

There are two extensions for Flask that simplify the use of websockets. The Flask-Socketsextension by Kenneth Reitz is a wrapper for gevent and gevent-websocket. The Flask-SocketIOextension (shameless plug as I'm the author) is a wrapper for gevent and gevent-socketio on the server, plus Socket.IOon the client. Socket.IO is higher level socket protocol that can use web socket if available but can also use other transport mechanisms on older browsers.

There are two extensions for Flask that simplify the use of websockets. The Flask-Socketsextension by Kenneth Reitz is a wrapper for gevent and gevent-websocket. The Flask-SocketIOextension (shameless plug as I'm the author) is a wrapper for gevent and gevent-socketio on the server, plus Socket.IOon the client. Socket.IO is higher level socket protocol that can use web socket if available but can also use other transport mechanisms on older browsers.

I hope this helps!

I hope this helps!