Flask中的Websocket [英] Websockets in Flask

查看:890
本文介绍了Flask中的Websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究Python中的websocket支持,并且对产品感到有些困惑.

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

一方面,可以使用 Flask + gevent .另一方面,uwsgi具有套接字支持,最后有一个将 uwsgi和gevent 捆绑在一起的扩展程序.

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

仅使用其中之一实施websockets有什么问题?混合它们能赢什么?

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

更改问题

添加gevent可以使线程化的uwsgi不能做什么?

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

推荐答案

在常规HTTP请求中,客户端和服务器之间的连接是短暂的,客户端连接到服务器,发送请求,接收响应,然后关闭联系.在此模型中,服务器可以使用少量工作程序为大量客户端提供服务.在这种情况下,并发模型通常基于线程,进程或两者的组合.

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.

使用websocket时,问题更加复杂,因为websocket连接长时间处于打开状态,因此服务器无法使用一小部分工作人员来服务大量客户端,每个客户端都需要获得自己敬业的工人.如果您使用线程和/或进程,则您的应用将无法扩展以支持大量客户端,因为您不能拥有大量线程/进程.

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.

这是gevent输入图片的地方. Gevent有一个基于greenlets的并发模型,它的扩展性比线程/进程好得多.因此,由于greenlets的轻量级特性,通过基于gevent的服务器为websocket连接提供服务可支持更多客户端.使用uWSGI,您可以选择用于Web套接字的并发模型,其中包括gevent中基于greenlet的模型.如果需要,您还可以独立使用gevent的Web服务器.

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.

但是请注意,gevent对Web套接字一无所知,它只是一台服务器.要使用websocket连接,您必须添加websocket服务器的实现.

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.

Flask有两个扩展,可简化websocket的使用. Kenneth Reitz的 Flask-Sockets 扩展程序是gevent和gevent-websocket的包装. Flask-SocketIO 扩展名(我为作者是无耻插件)是gevent的包装和服务器上的gevent-socketio,以及客户端上的 Socket.IO . Socket.IO是更高级别的套接字协议,可以使用Web套接字(如果可用),但也可以在较旧的浏览器上使用其他传输机制.

There are two extensions for Flask that simplify the use of websockets. The Flask-Sockets extension by Kenneth Reitz is a wrapper for gevent and 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. Socket.IO is higher level socket protocol that can use web socket if available but can also use other transport mechanisms on older browsers.

我希望这会有所帮助!

这篇关于Flask中的Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆