python flask websocket sleep_当前可用于python3上带有flask的websocket的最佳选择

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

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

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

解决方案

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.

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

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.

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:

Eventlet

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 and gevent-websocket

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 (or any other multi-threaded WSGI compatible server)

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.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值