Skip to content

Instantly share code, notes, and snippets.

@iamdionysus
Created November 14, 2013 05:49
Show Gist options
  • Save iamdionysus/7462062 to your computer and use it in GitHub Desktop.
Save iamdionysus/7462062 to your computer and use it in GitHub Desktop.
Flask + websocket from gevent.pywsgi + reloader. I wanted to use Flask for the micro web platform. Added websocket from gevent which needs pywsgi instead of wsgi Finally, for agile development, change should be made on the fly. Code snippet for werkzeug.serving is using gevent.wsgi instead of gevent.pywsgi. If you follow the snippet it causes th…
from gevent import monkey; monkey.patch_all()
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from flask import Flask, request, render_template
import werkzeug.serving
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/echo')
def echo():
if request.environ.get('wsgi.websocket'):
ws = request.environ['wsgi.websocket']
while True:
message = ws.receive()
ws.send(message)
return
@werkzeug.serving.run_with_reloader
def runServer():
app.debug = True
http_server = WSGIServer(('127.0.0.1', 5000),
app,
handler_class=WebSocketHandler)
http_server.serve_forever()
@wolfhechel
Copy link

werkzeug.serving.run_with_reloader basically takes your method and runs it inside a new thread, the main thread is then used to with an interval stat the filesystem for changes.

This in turn makes gevent misbehave because it doesn't get full runtime in the main thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment