flask-uwsgi-websocket

我要开发同款
匿名用户2021年12月10日
194阅读

技术信息

授权协议
MIT License

作品详情

Flask-uWSGI-WebSocket

High-performaceWebSocketsforyourFlaskappspoweredbyuWSGI.Low-leveluWSGIWebSocketAPIaccessadflexiblehigh-levelabstractiosforbuildigcomplexWebSocketapplicatioswithFlask.SupportsseveraldifferetcocurrecymodelsicludigGevet.IspiredbyFlask-Sockets.

fromflaskimportFlaskfromflask_uwsgi_websocketimportGevetWebSocketapp=Flask(__ame__)websocket=GevetWebSocket(app)@websocket.route('/echo')defecho(ws):whileTrue:msg=ws.receive()ws.sed(msg)if__ame__=='__mai__':app.ru(gevet=100)Istallatio

Preferredmethodofistallatioisviapip:

$pipistallFlask-uWSGI-WebSocketIstalliguWSGI

Ofcourseyou'llalsoeeduWSGI(withSSLsupport,atmiimum).Itcaalsobeistalledwithpip:

$pipistalluwsgi

Ifthatfailsoryoueedtoeabletheasycioplugi,reado.

uWSGIoMacOSX

OsomeversiosofMacOSX,OpeSSLheadersareologericluded.IfyouuseHomebrew,istallOpeSSLadesuretheyareavailable:

$brewistallopessl&&brewlikopessl--force

ThisshouldesurepipcaistalluWSGI:

$LDFLAGS="-L/usr/local/lib"pipistalluwsgi--o-use-wheel

Ifyouplatousetheasycioplugi,you'lleedtoesurethatit'seabledwheuWSGIiscompiled.YoucauseUWSGI_PROFILEtodothis.WithHomebrewPytho3.5istalled:

$LDFLAGS="-L/usr/local/lib"CFLAGS="-I/usr/local/iclude/pytho3.5m"UWSGI_PROFLILE="asycio"pip3istalluwsgi--o-use-wheeluWSGIoLiux

IfyourLiuxdistributioicludesuWSGIwithspecificplugis,thatismaytimesyourbestbet.Ifthatfailsoryou'dprefertocompileuWSGIyourself,you'lleedtoesurethattherequisitebuildtools,OpeSSLheaders,etcareistalled:

$apt-getistallbuild-essetiallibssl-devpytho3-devpytho3-vev

AccordigtotheuWSGIasyciodocs,UWSGI_PROFILEadgreelet.hlocatioshouldbespecified.

IfyouareistalliguWSGIitoavirtualev,theprocessis:

$pytho3-mvevpyvev$.pyvev/bi/activate(pyvev)$pipistallgreelet

Now,greelet.hshouldbeavailableat$VIRTUAL_ENV/iclude/site/pytho3.5.Tobuildwithpip:

$mkdir-p$VIRTUAL_ENV/iclude/site/pytho3.5/greelet$l-s../greelet.h$VIRTUAL_ENV/iclude/site/pytho3.5/greelet/$CFLAGS="-I$VIRTUAL_ENV/iclude/site/pytho3.5"UWSGI_PROFILE="asycio"pipistalluwsgi--o-use-wheelDeploymet

YoucauseuWSGI'sbuilt-iHTTProutertogetupadruigquickly:

$uwsgi--master--http:8080--http-websockets--wsgiecho:app

...whichiswhatapp.rudoesafterwrappigyourFlaskapp:

app.ru(debug=True,host='localhost',port=8080,master=true,processes=8)

uWSGIsupportsseveralcocurrecymodels,iparticularithasicesupportforGevet.IfyouwattouseGevet,importflask_uwsgi_websocket.GevetWebSocketadcofigureuWSGItousethegevetloopegie:

$uwsgi--master--http:8080--http-websockets--gevet100--wsgiecho:app

...or:

app.ru(debug=True,gevet=100)

Notethatyoucaotusemultiplethreadswithgevetloopegie.

Toeableasycioistead:

$uwsgi--master--http:5000--http-websockets--asycio100--greelet--wsgichat:app

...or:

app.ru(debug=True,asycio=100,greelet=True)

Forproductioyou'llprobablywattoruuWSGIbehidHaproxyorNgix,isteadofusigthebuilt-itHTTProuter.ExploretheuWSGIdocumetatiotolearmoreaboutthevariouscocurrecyaddeploymetoptios.

Developmet

It'spossibletotakeadvatageofFlask'siteractivedebuggerbyistalligWerkzeug'sDebuggedApplicatiomiddleware:

fromwerkzeug.debugimportDebuggedApplicatioapp.wsgi_app=DebuggedApplicatio(app.wsgi_app,True)

...adruiguWSGIwitholyasigleworker:

$uwsgi--master--http:8080--http-websockets--wsgi-file--workers1--threads8app.py

Ifyouuseapp.ru(debug=True)orexportFLASK_UWSGI_DEBUG,Flask-uWSGI-Websocketwilldothisautomaticallyforyou.

Examples

Thereareseveralexamplesavailablehere.

APIWebSocket

AppliesWebSocketMiddlewaretoyourFlaskApp,allowigyoutodecoraterouteswiththeroutemethod,turigthemitoWebSockethadlers.

Additioallymokey-patchesapp.ru,toruyourappdirectlyiuWSGI.

route(url)

ru(debug,host,port,**kwargs)**kwargsarepassedtouWSGIascommadlieargumets.

WebSocketMiddleware

WebSocketMiddlewarewhichautomaticallyperformsWebSockethadshakeadpassesWebSocketClietistacestoyourroute.

WebSocketCliet

ExposestheuWSGIWebSocketAPI.

recv()(aliasWebSocket.receive())

recv_b()

sed(msg)

sed_biary(msg)

recv_b()

sed_from_sharedarea(id,pos)

sed_biary_from_sharedarea(id,pos)

GevetWebSocket

FacierWebSocketabstractiothattakesadvatageofGevetloopegie.RequiresuWSGItoberuwith--uwsgioptio.

GevetWebSocketMiddleware

AutomaticallyperformsWebSockethadshakeadpassesaGevetWebSocketClietistacetoyourroute.

GevetWebSocketCliet

WebSocketclietabstractiowithfullyo-blockigmethods.

receive()

sed(msg)

close()

coected

AsycioWebSocket

FacierWebSocketabstractiothattakesadvatageofAsycioloopegie.RequiresuWSGItoberuwith--asycioad--greeletoptio.

AsycioWebSocketMiddleware

AutomaticallyperformsWebSockethadshakeadpassesaAsycioWebSocketClietistacetoyourroute.

AsycioWebSocketCliet

WebSocketclietabstractiowithasyciocorouties.

coroutiea_recv()(aliasreceive(),recv())

coroutiea_sed(msg)(aliassed())

recv_b()(shouldbeuseless)

sed_b()(shouldbeuseless)

close()

coected

AdvacedUsage

Normallywebsocketrouteshappeoutsideoftheormalrequestcotext.Youcagetarequestcotextiyourwebsockethadlerbyusigapp.request_cotext:

app=Flask(__ame__)ws=GevetWebSocket(app)@ws.route('/websocket')defwebsocket(ws):withapp.request_cotext(ws.eviro):pritrequest.args

功能介绍

Flask-uWSGI-WebSocket High-performance WebSockets for your Flask apps powered by uWSGI. Low-level...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论