当前可用于python3上带有flask的websocket的最佳选择 [英] Best option currently available for websockets with flask on python3

查看:581
本文介绍了当前可用于python3上带有flask的websocket的最佳选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我看到以下可用于在烧瓶中使用websocket的库:

currently i see the following libraries available for using websockets with flask:

  1. 烧瓶-插座(可能已过时)
  2. flask-SocketIO
  3. gevent-websockets (高于2取决于此)
  1. flask-sockets (possibly obsolete)
  2. flask-SocketIO
  3. gevent-websockets (above 2 depend on this)

但是gevent-websockets尚不兼容python3(至少不能通过pip安装).这使得其他2个也无法使用.

However gevent-websockets is not yet python3 compatible(atleast it is not install-able via pip). Which makes the other 2 unusable as well.

还有其他将烧瓶用于websockets的替代方法吗?还是龙卷风是最佳选择?

Is there any other alternative for using websockets with flask? Or is tornado the best choice?

推荐答案

首先,请注意Flask-SocketIO与其他两个不同.它包装Socket.IO协议,而不包装原始WebSocket.这样一来,它就可以为不支持WebSocket协议的客户端支持HTTP HTTP长轮询模拟的类似WebSocket的接口.

First of all, note that Flask-SocketIO is different from the other two. It wraps the Socket.IO protocol, not raw WebSocket. This enables it to support a WebSocket-like interface emulated with HTTP long-polling for clients that do not support the WebSocket protocol.

谈到Flask-SocketIO,请注意,此扩展程序将进行重大改进.

Speaking of Flask-SocketIO, note that this extension is about to receive a major overhaul.

当前的正式版本(0.6)取决于gevent,gevent-socketio和gevent-websocket.不幸的是,gevent-socketio作为一个项目早已不复存在,并且停留在Socket.IO协议的非常旧的版本上,这迫使您使用古老的Javascript客户端.

The current official release (0.6) depends on gevent, gevent-socketio and gevent-websocket. Unfortunately gevent-socketio as a project has long been dead and is stuck on a very old version of the Socket.IO protocol, which forces you to use an ancient Javascript client.

即将发布的Flask-SocketIO版本1.0是一个完全重写,主要与0.6版本兼容,但是它支持Python 2和Python 3,并且采用了允许将不同机制用于联网和并发的设计.版本0.6中的gevent-socketio依赖关系被两个名为 python-socketio python-engineio ,它实现了Socket.IO协议的当前版本,并支持所有正式的Socket. IO客户端(JavaScript,Swift和C ++).

The upcoming Flask-SocketIO release 1.0 is a complete rewrite that is mostly compatible with the 0.6 release, but it has support for Python 2 and Python 3 and sports a design that allows different mechanisms to be used for networking and concurrency. The gevent-socketio dependency from version 0.6 is replaced with two new packages called python-socketio and python-engineio, which implement the current version of the Socket.IO protocol, and support all the official Socket.IO clients (Javascript, Swift and C++).

至于底层依赖性,您基本上有三个选择:

As far as low-level dependencies, you have basically three options:

  1. Eventlet

  1. Eventlet

在Py2和Py3上,Eventlet是迄今为止性能最高且最稳定的选项. WebSocket支持是eventlet固有的功能,因此只需在virtualenv中安装eventlet即可使Flask-SocketIO在最佳配置下工作.

Eventlet is by far the most performant and stable option, on both Py2 and Py3. WebSocket support is native to eventlet, so just installing eventlet in your virtualenv enables Flask-SocketIO to work in the most optimal configuration.

gevent和gevent-websocket

gevent and gevent-websocket

如果出于某种原因您更喜欢gevent,那也可以,但是在我的测试中,它的性能不如eventlet.如果要在使用gevent时访问WebSocket,则还需要安装软件包gevent-websocket. gevent和gevent-websocket都已移植到Python 3,但这都太新了.

If for any reason you prefer gevent, that also works, but in my tests it does not perform as good as eventlet. If you want to have access to WebSocket when using gevent, then you also need to install package gevent-websocket. Both gevent and gevent-websocket have been ported to Python 3, but it is all too recent.

Werkzeug(或任何其他多线程WSGI兼容服务器)

Werkzeug (or any other multi-threaded WSGI compatible server)

您还可以使用Flask自己的Web服务器.在此模式下,扩展名仅在长轮询模式下工作.与其他两个相比,性能显然很糟糕,但是对于开发和调试而言,能够使所有工作正常运行而无需运行eventlet或gevent以及它们带来的所有复杂性是一件很不错的事情.

You can also use Flask's own web server. In this mode, the extension only works in long-polling mode. Performance is obviously terrible compared to the other two, but for development and debugging it is nice to be able to get everything working without having to run eventlet or gevent, with all the complexities they bring.

截止到今天(2015年4月4日),新的Flask-SocketIO扩展具有功能齐全的beta版本.即将发布正式版本.您可以通过以下方式安装测试版:

As of today (10/4/2015), there is a fully functional beta release of the new Flask-SocketIO extension. An official release is coming soon. You can install the beta release with:

pip install flask-socketio==1.0b1

该代码在git repo的1.0分支上可用: https://github.com/miguelgrinberg/Flask-SocketIO/tree/v1.0 .

The code is available on the 1.0 branch of the git repo: https://github.com/miguelgrinberg/Flask-SocketIO/tree/v1.0.

免责声明:我敢肯定,现在已经很清楚了,但以防万一,请注意,我是Flask-SocketIO,python-socketio和python-engineio的作者.

Disclaimer: I'm sure this is clear by now, but just in case, note that I'm the author of Flask-SocketIO, python-socketio and python-engineio.

这篇关于当前可用于python3上带有flask的websocket的最佳选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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