gunicorn和websockets

时间:2012-02-25 13:28:30

标签: python websocket gevent gunicorn

我正在尝试http://www.gelens.org/code/gevent-websocket/运行并继续收到以下错误。

socket_id=1 already closed.
    result = self._run(*self.args, **self.kwargs)
  File "/home/walt/virtualenv/ws/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 571, in handle
    handler.handle()
  File "/home/walt/virtualenv/ws/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 180, in handle
    result = self.handle_one_request()
  File "/home/walt/virtualenv/ws/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 314, in handle_one_request
    self.handle_one_response()
  File "/home/walt/virtualenv/ws/local/lib/python2.7/site-packages/geventwebsocket/handler.py", line 26, in handle_one_response
    return self._handle_websocket()
  File "/home/walt/virtualenv/ws/local/lib/python2.7/site-packages/geventwebsocket/handler.py", line 50, in _handle_websocket
    self.log_request()
  File "/home/walt/virtualenv/ws/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 394, in log_request
    log.write(self.format_request() + '\n')
AttributeError: 'Logger' object has no attribute 'write'

我在Mint Linux上运行python 2.7.2

3 个答案:

答案 0 :(得分:8)

使用@ bkad的代码并添加到主要主应用程序文件

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

以便我不必修改第三方库。

答案 1 :(得分:3)

运行gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" example:app示例时,我遇到了与gevent-websocket 0.3.4和gevent 0.13.6相同的错误。

问题来自于gevent和gunicorn之间的期望不匹配。 Gevent的pywsgi模块需要一个实现write方法的logger对象,比如文件对象,stdout等。然而,Gunicorn传入一个具有Logger方法的对象(调试,信息,错误,关键,警告等)。

pywsgi中的问题代码:

def log_request(self):
    log = self.server.log
    if log:
        log.write(self.format_request() + '\n')

为了解决这个问题,我在一个gevent的分支中修改了这些行并将它们更改为以下内容:

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')

答案 2 :(得分:1)

请在PyPi上使用最新版本的gevent和gevent-websocket。