10

Is it possible to use Gunicorn to handle WebSockets and regular WSGI views together in one (Flask) app?

I know how to get websockets to work using the Gevent WSGI server, and I can get a regular WSGI app running with Gunicorn, with gevent workers, but when I try to serve the two together from one app using Gunicorn I get an error:

ValueError: View function did not return a response

Is it possible to serve the two from one app, using gunicorn? I plan eventually to put this all behind nginx, and I'm not averse to splitting the socket into another app and having the two communicate, as long as that doesn't demand too many additional system resources. Until then, is there a way to do it this way?

EDIT:

I figured out how to get this working. The key is 1) change the logging function for gevent and 2) make sure to specify to gunicorn that I'm using geventWebSocketWorker class workers.

I found part of this answer on this site: http://d.hatena.ne.jp/Malan/20121007

For the record, I think it's probably a better idea to have one server running tornado/twisted/autobahn(thanks Jordan) and another running my WSGI stuff. But that's not what I wanted here :)

def log_request(self):
    log = self.server.log
    if log:
        if hasattr(log, "info"):
            log.info(self.format_request() + '\n')
        else:
            log.write(self.format_request() + '\n')

import gevent        
gevent.pywsgi.WSGIHandler.log_request = log_request
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer

sudo gunicorn -c gunicorn_config.py -k     "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" router:app       
5
  • 1
    Is this related for what you're doing? stackoverflow.com/questions/13137449/…
    – Jordan
    Jul 14, 2013 at 5:03
  • Absolutely, but I want to avoid using twisted or tornado with this project, if possible. Jul 22, 2013 at 0:25
  • You should ditch gunicorn in favor of uwsgi.
    – user37203
    Nov 19, 2014 at 9:20
  • @user37203 Can you give more context for that suggestion? Nov 19, 2014 at 17:13
  • 2
    uwsgi is faster, more stable and smaller than gunicorn. it is a wsgi server written in C which can run python wsgi apps in a number of ways. Nginx has a uwsgi backend, and this is the suggested way to run it. ngnix -> uwsgi -> python app Just try it out, it's slowly becoming industry standard.
    – user37203
    Nov 20, 2014 at 13:55

1 Answer 1

4

Flask-Sockets might be helpful.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.