我正在尝试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
使用@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
以便我不必修改第三方库。
在运行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方法(debug、info、error、critical、warning等)的对象。
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')
请在PyPi上使用最新版本的gevent和gevent websocket。