简体   繁体   English

Flask中的Websocket

[英]Websockets in Flask

I'm currently researching websocket support in Python and am a bit confused with the offerings. 我目前正在研究Python中的websocket支持,并且对这些产品有些困惑。

On one hand it's possible to use Flask + gevent . 一方面,可以使用Flask + gevent On the other hand, uwsgi has socket support and at last there is an extension that bundles both uwsgi and gevent . 另一方面,uwsgi具有套接字支持 ,最后有一个扩展将uwsgi和gevent捆绑在一起

What's the problem with implementing websockets with only one of these? 仅使用其中之一实施websockets有什么问题? What do I win by mixing them? 混合它们能赢什么?

Changing the question 改变问题

What does adding gevent do that threaded uwsgi won't? 添加gevent不会使线程化的uwsgi做什么?

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. 在常规HTTP请求中,客户端和服务器之间的连接是短暂的,客户端连接到服务器,发送请求,接收响应,然后关闭连接。 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. 使用websocket时,问题更加复杂,因为websocket连接已打开很长时间,因此服务器无法使用一小部分工作人员来服务大量客户端,每个客户端都需要拥有自己的专用工作人员。 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输入图片的地方。 Gevent has a concurrency model based on greenlets, which scale much better than threads/processes. Gevent有一个基于greenlets的并发模型,它的扩展性比线程/进程好得多。 So serving websocket connections with a gevent based server allows you support more clients, due to the lightweight nature of greenlets. 因此,由于greenlets的轻量性质,通过基于gevent的服务器为websocket连接提供服务可支持更多客户端。 With uWSGI you have a choice of concurrency models to use with web sockets, and that includes the greenlet based model from gevent. 使用uWSGI,您可以选择与Web套接字一起使用的并发模型,其中包括gevent中基于greenlet的模型。 You can also use gevent's web server standalone if you want. 如果需要,您也可以独立使用gevent的Web服务器。

But note that gevent does not know anything about web sockets, it is just a server. 但是请注意,gevent对Web套接字一无所知,它只是一台服务器。 To use websocket connections you have to add an implementation of the websocket server. 要使用websocket连接,您必须添加websocket服务器的实现。

There are two extensions for Flask that simplify the use of websockets. Flask有两个扩展,可简化websocket的使用。 The Flask-Sockets extension by Kenneth Reitz is a wrapper for gevent and gevent-websocket. Kenneth Reitz的Flask-Sockets扩展是gevent和gevent-websocket的包装。 The Flask-SocketIO extension (shameless plug as I'm the author) is a wrapper for gevent and gevent-socketio on the server, plus Socket.IO on the client. Flask-SocketIO扩展(我是无耻的插件)是服务器上gevent和gevent-socketio以及客户端上Socket.IO的包装 Socket.IO is higher level socket protocol that can use web socket if available but can also use other transport mechanisms on older browsers. Socket.IO是更高级别的套接字协议,可以使用Web套接字(如果可用),但也可以在较旧的浏览器上使用其他传输机制。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM