Skip to content

Instantly share code, notes, and snippets.

@sigmaris
Created May 12, 2014 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigmaris/e60c38480b5ada85d51b to your computer and use it in GitHub Desktop.
Save sigmaris/e60c38480b5ada85d51b to your computer and use it in GitHub Desktop.
uwsgi websocket test
# Run this with: uwsgi --virtualenv venv --socket /tmp/uwsgi.sock --chmod-socket --plugin gevent --gevent 1000 --gevent-monkey-patch --module echo:application --master --processes 4 --enable-threads
from flask import Flask, request, render_template
import gevent
import logging
from uwsgi_websockets.middleware import ws_middleware
import uwsgi
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.debug = True
@app.route('/')
def index():
return render_template('echo.html')
@app.route('/websocket')
def websocket():
worker_id = uwsgi.worker_id()
logger.debug('{0} is handling a request in {1}'.format(worker_id, gevent.getcurrent()))
if request.environ.get('wsgi.websocket'):
ws = request.environ['wsgi.websocket']
while True:
message = ws.receive()
if message:
logger.debug('Echo {0}'.format(message))
ws.send(message)
else:
logger.debug("{0} received nothing in {1}!".format(worker_id, gevent.getcurrent()))
return ''
application = ws_middleware(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment