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)IstallatioPreferredmethodofistallatioisviapip:
$pipistallFlask-uWSGI-WebSocketIstalliguWSGIOfcourseyou'llalsoeeduWSGI(withSSLsupport,atmiimum).Itcaalsobeistalledwithpip:
$pipistalluwsgiIfthatfailsoryoueedtoeabletheasycioplugi,reado.
uWSGIoMacOSXOsomeversiosofMacOSX,OpeSSLheadersareologericluded.IfyouuseHomebrew,istallOpeSSLadesuretheyareavailable:
$brewistallopessl&&brewlikopessl--forceThisshouldesurepipcaistalluWSGI:
$LDFLAGS="-L/usr/local/lib"pipistalluwsgi--o-use-wheelIfyouplatousetheasycioplugi,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-wheeluWSGIoLiuxIfyourLiuxdistributioicludesuWSGIwithspecificplugis,thatismaytimesyourbestbet.Ifthatfailsoryou'dprefertocompileuWSGIyourself,you'lleedtoesurethattherequisitebuildtools,OpeSSLheaders,etcareistalled:
$apt-getistallbuild-essetiallibssl-devpytho3-devpytho3-vevAccordigtotheuWSGIasyciodocs,UWSGI_PROFILEadgreelet.hlocatioshouldbespecified.
IfyouareistalliguWSGIitoavirtualev,theprocessis:
$pytho3-mvevpyvev$.pyvev/bi/activate(pyvev)$pipistallgreeletNow,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-wheelDeploymetYoucauseuWSGI'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.
DevelopmetIt'spossibletotakeadvatageofFlask'siteractivedebuggerbyistalligWerkzeug'sDebuggedApplicatiomiddleware:
fromwerkzeug.debugimportDebuggedApplicatioapp.wsgi_app=DebuggedApplicatio(app.wsgi_app,True)...adruiguWSGIwitholyasigleworker:
$uwsgi--master--http:8080--http-websockets--wsgi-file--workers1--threads8app.pyIfyouuseapp.ru(debug=True)orexportFLASK_UWSGI_DEBUG,Flask-uWSGI-Websocketwilldothisautomaticallyforyou.
ExamplesThereareseveralexamplesavailablehere.
APIWebSocketAppliesWebSocketMiddlewaretoyourFlaskApp,allowigyoutodecoraterouteswiththeroutemethod,turigthemitoWebSockethadlers.
Additioallymokey-patchesapp.ru,toruyourappdirectlyiuWSGI.
route(url)
ru(debug,host,port,**kwargs)**kwargsarepassedtouWSGIascommadlieargumets.
WebSocketMiddlewareWebSocketMiddlewarewhichautomaticallyperformsWebSockethadshakeadpassesWebSocketClietistacestoyourroute.
WebSocketClietExposestheuWSGIWebSocketAPI.
recv()(aliasWebSocket.receive())
recv_b()
sed(msg)
sed_biary(msg)
recv_b()
sed_from_sharedarea(id,pos)
sed_biary_from_sharedarea(id,pos)
GevetWebSocketFacierWebSocketabstractiothattakesadvatageofGevetloopegie.RequiresuWSGItoberuwith--uwsgioptio.
GevetWebSocketMiddlewareAutomaticallyperformsWebSockethadshakeadpassesaGevetWebSocketClietistacetoyourroute.
GevetWebSocketClietWebSocketclietabstractiowithfullyo-blockigmethods.
receive()
sed(msg)
close()
coected
AsycioWebSocketFacierWebSocketabstractiothattakesadvatageofAsycioloopegie.RequiresuWSGItoberuwith--asycioad--greeletoptio.
AsycioWebSocketMiddlewareAutomaticallyperformsWebSockethadshakeadpassesaAsycioWebSocketClietistacetoyourroute.
AsycioWebSocketClietWebSocketclietabstractiowithasyciocorouties.
coroutiea_recv()(aliasreceive(),recv())
coroutiea_sed(msg)(aliassed())
recv_b()(shouldbeuseless)
sed_b()(shouldbeuseless)
close()
coected
AdvacedUsageNormallywebsocketrouteshappeoutsideoftheormalrequestcotext.Youcagetarequestcotextiyourwebsockethadlerbyusigapp.request_cotext:
app=Flask(__ame__)ws=GevetWebSocket(app)@ws.route('/websocket')defwebsocket(ws):withapp.request_cotext(ws.eviro):pritrequest.args






评论