如何处理龙卷风中的线程异常?

时间:2013-08-15 12:52:14

标签: python-3.x tornado python-multithreading

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

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)

1 个答案:

答案 0 :(得分:1)

看起来你在数组中有不同的监听器。很难说出问题的确切原因,但试着像这样记录下来:

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

它可以防止错误。