UWSGi提供的Flask应用程序的高性能WebSocket。
Flask-uWSGI-WebSocket的Python项目详细描述
由uWSGI提供支持的Flask应用程序的高性能WebSocket。低层uwsgi websocket api 用于构建复杂WebSub的访问和灵活高层抽象 使用烧瓶。支持几种不同的并发模型 包括gevent。灵感来自Flask-Sockets。
fromflaskimportFlaskfromflask_uwsgi_websocketimportGeventWebSocketapp=Flask(__name__)websocket=GeventWebSocket(app)@websocket.route('/echo')defecho(ws):whileTrue:msg=ws.receive()ws.send(msg)if__name__=='__main__':app.run(gevent=100)
安装
首选的安装方法是通过pip:
$ pip install Flask-uWSGI-WebSocket
安装uwsgi
当然,您还需要uwsgi(至少有ssl支持)。也可以是 与PIP一起安装:
$ pip install uwsgi
如果失败或需要启用Asyncio插件,请继续阅读。
mac os x上的uwsgi
在某些版本的mac os x上,openssl头不再包含。如果你 使用自制程序,安装OpenSSL并确保它们可用:
$ brew install openssl && brew link openssl --force
这将确保pip可以安装uwsgi:
$ LDFLAGS="-L/usr/local/lib" pip install uwsgi --no-use-wheel
如果计划使用Asyncio插件,则需要确保它已启用 在编译uwsgi时。您可以使用UWSGI_PROFILE来执行此操作。安装了自制Python 3.5后:
$ LDFLAGS="-L/usr/local/lib" CFLAGS="-I/usr/local/include/python3.5m" UWSGI_PROFLILE="asyncio" pip3 install uwsgi --no-use-wheel
linux上的uwsgi
如果您的linux发行版包含带有特定插件的uwsgi,那么 是你最好的选择。如果失败了或者你更愿意自己编译uwsgi, 您需要确保必需的构建工具、openssl头文件等 已安装:
$ apt-get install build-essential libssl-dev python3-dev python3-venv
根据uWSGI asyncio docs,UWSGI_PROFILE 应该指定greenlet.h位置。
如果要将uwsgi安装到virtualenv中,过程是:
$ python3 -m venv pyvenv $ . pyvenv/bin/activate (pyvenv)$ pip install greenlet
现在,greenlet.h应该在$VIRTUAL_ENV/include/site/python3.5上可用。使用pip构建:
$ mkdir -p $VIRTUAL_ENV/include/site/python3.5/greenlet $ ln -s ../greenlet.h $VIRTUAL_ENV/include/site/python3.5/greenlet/ $ CFLAGS="-I$VIRTUAL_ENV/include/site/python3.5" UWSGI_PROFILE="asyncio" pip install uwsgi --no-use-wheel
展开
您可以使用uwsgi的内置http路由器快速启动和运行:
$ uwsgi --master --http :8080 --http-websockets --wsgi echo:app
…这就是app.run在包装您的烧瓶应用程序后所做的工作:
app.run(debug=True, host='localhost', port=8080, master=true, processes=8)
uwsgi支持多个并发模型,特别是它有很好的支持。 对于gevent。如果要使用gevent,请导入 flask_uwsgi_websocket.GeventWebSocket并将uwsgi配置为使用 gevent循环发动机:
$ uwsgi --master --http :8080 --http-websockets --gevent 100 --wsgi echo:app
…或:
app.run(debug=True, gevent=100)
注意,不能在gevent循环引擎中使用多个线程。
要改为启用异步,请执行以下操作:
$ uwsgi --master --http :5000 --http-websockets --asyncio 100 --greenlet --wsgi chat:app
…或:
app.run(debug=True, asyncio=100, greenlet=True)
在制作过程中,你可能需要在haproxy或nginx之后运行uwsgi, 而不是使用内置的int http路由器。探索uWSGI documentation以了解更多 关于各种并发和部署选项。
开发
可以通过安装 Werkzeug的DebuggedApplication中间件:
from werkzeug.debug import DebuggedApplication app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
…运行只有一个工人的uwsgi:
$ uwsgi --master --http :8080 --http-websockets --wsgi-file --workers 1 --threads 8 app.py
如果使用app.run(debug=True)或导出FLASK_UWSGI_DEBUG, Flask Uwsgi WebSocket将为您自动执行此操作。
示例
这里有几个例子available here。
API
WebSocket
将WebSocketMiddleware应用于烧瓶应用程序,允许您装饰 使用route方法路由,将它们转换为WebSocket处理程序。
另外,monkey补丁app.run,可以直接在uwsgi中运行应用程序。
route(url)
run(debug, host, port, **kwargs)**kwargs作为命令行参数传递给uwsgi。
WebSocketMiddleware
自动执行WebSocket握手和 将WebSocketClient实例传递到您的路由。
WebSocketClient
recv()(别名WebSocket.receive())
recv_nb()
send(msg)
send_binary(msg)
recv_nb()
send_from_sharedarea(id, pos)
send_binary_from_sharedarea(id, pos)
GeventWebSocket
利用gevent循环引擎的更奇特的websocket抽象。 需要使用--uwsgi选项运行uwsgi。
GeventWebSocketMiddleware
自动执行WebSocket握手并通过 GeventWebSocketClient实例到您的路由。
GeventWebSocketClient
完全非bloc的websocket客户机抽象王道。
receive()
send(msg)
close()
connected
AsyncioWebSocket
利用异步循环引擎的更奇特的WebSocket抽象。 需要使用--asyncio和--greenlet选项运行uwsgi。
AsyncioWebSocketMiddleware
自动执行WebSocket握手并将^{TT42}$实例传递到路由。
AsyncioWebSocketClient
带有异步协同路由的WebSocket客户端抽象。
coroutine a_recv()(别名receive(),recv())
coroutine a_send(msg)(别名send())
recv_nb()(应该没用)
send_nb()(应该没用)
close()
connected
高级用法
通常WebSocket路由发生在正常请求上下文之外。你可以 通过使用 app.request_context:
app = Flask(__name__) ws = GeventWebSocket(app) @ws.route('/websocket') def websocket(ws): with app.request_context(ws.environ): print request.args