From e4ccb1c43ffdee1fcbf6a61daa56d7607951d39d Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 3 Nov 2015 23:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E7=9A=84=E5=BC=82=E5=B8=B8bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run_log_watch.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/run_log_watch.py b/run_log_watch.py index 3694a3030..12fb83a14 100644 --- a/run_log_watch.py +++ b/run_log_watch.py @@ -22,6 +22,17 @@ define("port", default=8080, help="run on the given port", type=int) define("host", default='0.0.0.0', help="run port on", type=str) +class MyThread(threading.Thread): + def __init__(self, *args, **kwargs): + super(MyThread, self).__init__(*args, **kwargs) + + def run(self): + try: + super(MyThread, self).run() + except WebSocketClosedError: + pass + + class EventHandler(ProcessEvent): def __init__(self, client=None): self.client = client @@ -34,10 +45,7 @@ class EventHandler(ProcessEvent): def process_IN_MODIFY(self, event): print "Modify file:%s." % os.path.join(event.path, event.name) - try: - self.client.write_message(f.read()) - except WebSocketClosedError: - raise WebSocketClosedError + self.client.write_message(f.read()) def file_monitor(path='.', client=None): @@ -99,7 +107,7 @@ class MonitorHandler(tornado.websocket.WebSocketHandler): # 获取监控的path self.file_path = self.get_argument('file_path', '') MonitorHandler.clients.append(self) - thread = threading.Thread(target=file_monitor, args=('%s.log' % self.file_path, self)) + thread = MyThread(target=file_monitor, args=('%s.log' % self.file_path, self)) MonitorHandler.threads.append(thread) self.stream.set_nodelay(True) @@ -116,7 +124,7 @@ class MonitorHandler(tornado.websocket.WebSocketHandler): t.setDaemon(True) t.start() - except WebSocketClosedError, e: + except WebSocketClosedError: client_index = MonitorHandler.clients.index(self) MonitorHandler.threads[client_index].stop() MonitorHandler.clients.remove(self)