-1

我正在使用白板应用程序。当我第一次加载页面时,它给出了完美的输出,第二次也给出了完美的输出。但是当我第三次刷新页面时,它会出现如下错误,如下所示,

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.2/threading.py", line 693, in run
    self._target(*self._args, **self._kwargs)
  File "/home/nyros/Desktop/python3/whiteboard/websockethandler.py", line 45, in redis_listener
    listener.send_message(message['data'])
  File "/home/nyros/Desktop/python3/whiteboard/websockethandler.py", line 150, in send_message
    self.write_message(message)
  File "/home/nyros/Desktop/python3/venv3/lib/python3.2/site-packages/tornado/websocket.py", line 165, in write_message
    self.ws_connection.write_message(message, binary=binary)
AttributeError: 'NoneType' object has no attribute 'write_message' 

代码如下:

def redis_listener(self, room_name, page_no):
        self.logger.info("Starting listener thread for room %s" % room_name)
        rr = redis.Redis(host=config.REDIS_IP_ADDRESS, port=config.REDIS_PORT, db=1)
        r = rr.pubsub()
        r.subscribe(self.construct_key(room_name, page_no))

        for message in r.listen():
            print(message, "ok ok ok .......")
            for listener in self.application.LISTENERS.get(room_name, {}).get(page_no, []):
                self.logger.debug("Sending message to room %s" % room_name)
                listener.send_message(message['data'])

def send_message(self, message):
        if type(message) == type(b''):
            self.logger.info("Decoding binary string")
            message = message.decode('utf-8')
        elif type(message) != type(''):
            self.logger.info("Converting message from %s to %s" % (type(message),
                                                            type('')))
            message = str(message)
        message = b64encode(compress(bytes(quote(message), 'utf-8'), 9))
        self.write_message(message)
4

1 回答 1

1

看起来您在数组中有不同的侦听器。很难说出问题的确切原因,但请尝试像这样记录它们:

if hasattr(self, 'write_message'):
    self.write_message(message)
else: 
    self.logger.debug(self)

它将防止错误。

于 2013-08-19T06:24:53.440 回答