简体   繁体   中英

WebSockets and WSGI together through Gunicorn

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       

Flask-Sockets可能会有所帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM