python – gunicorn和websockets

我正在尝试让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

最佳答案
当运行gunicorn -k“geventwebsocket.gunicorn.workers.GeventWebSocketWorker”示例时,我得到了与gevent-websocket 0.3.4和gevent 0.13.6相同的错误:app example.

问题来自于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')

转载注明原文:python – gunicorn和websockets - 代码日志