小编Mos*_*ner的帖子

WebSockets和WSGI一起通过Gunicorn

是否可以使用Gunicorn在一个(Flask)应用程序中一起处理WebSockets和常规WSGI视图?

我知道如何使用Gevent WSGI服务器使websockets工作,我可以使用gevent worker获得与Gunicorn一起运行的常规WSGI应用程序,但是当我尝试使用Gunicorn从一个应用程序一起服务这两个时,我得到一个错误:

ValueError:View函数未返回响应

是否可以使用gunicorn从一个应用程序服务这两个?我计划最终将这一切都放在nginx之后,而且我不反对将套接字分成另一个应用程序并让两者进行通信,只要这不需要太多额外的系统资源.在那之前,有没有办法这样做?

编辑:

我想出了如何使这个工作.关键是1)更改gevent的日志记录功能和2)确保指定gunicorn我正在使用geventWebSocketWorker类worker.

我在这个网站上找到了这个答案的一部分:http: //d.hatena.ne.jp/Malan/20121007

为了记录,我认为让一台服务器运行龙卷风/ twisted/autobahn(感谢Jordan)和另一台运行我的WSGI东西可能更好.但这不是我想要的:)

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       
Run Code Online (Sandbox Code Playgroud)

python websocket gevent flask gunicorn

9
推荐指数
1
解决办法
2360
查看次数

标签 统计

flask ×1

gevent ×1

gunicorn ×1

python ×1

websocket ×1