diff --git a/f7/app.py b/f7/app.py index b113d36..e8cf826 100644 --- a/f7/app.py +++ b/f7/app.py @@ -23,6 +23,7 @@ class _App: return cls._instance def __init__(self): + self._listened = False self._requestHandler = {} self._app = tornado.web.Application([ (r"/webapp/index[\.]php", _mainHandler), @@ -34,10 +35,14 @@ class _App: f7.udplog.setLogDirAndCreate(log_dir) def start(self): - tornado.ioloop.IOLoop.current().start() + if self._listened: + tornado.ioloop.IOLoop.current().start() + f7.timer.unInit() + f7.udplog.unInit() def listen(self, port): self._app.listen(port) + self._listened = True def callAt(self, when, callback): tornado.ioloop.IOLoop.current().call_at(when, callback) diff --git a/f7/timer.py b/f7/timer.py index b92697e..d776b8f 100644 --- a/f7/timer.py +++ b/f7/timer.py @@ -15,6 +15,9 @@ class _Timer: def __init__(self): pass + def unInit(self): + pass + def callAt(self, when, callback): f7.app.callAt(when, callback) diff --git a/f7/udplog.py b/f7/udplog.py index 9a3a07f..3148e09 100644 --- a/f7/udplog.py +++ b/f7/udplog.py @@ -4,7 +4,6 @@ import os import time import datetime import traceback -import _thread import threading import q7 @@ -28,19 +27,26 @@ class _UdpLog: self._topNode = None self._botNode = None self._workNode = None + self._terminated = False + self._cond = threading.Condition() self._msgLock = threading.Lock() + self._workerThread = threading.Thread(target = self._saveToFile, args = ()) def init(self): - _thread.start_new_thread(self._saveToFile, ()) + self._workerThread.start() + + def unInit(self): + self._terminated = True + self._cond.acquire() + self._cond.notifyAll() + self._cond.release() + self._workerThread.join() def setLogDirAndCreate(self, logdir): if not os.path.exists(logdir): os.makedirs(logdir) self._logDir = logdir - def unInit(self): - pass - def info(self, msg): self._addLog(str(datetime.datetime.now()) + '[INFO]' + str(msg)) @@ -71,28 +77,34 @@ class _UdpLog: def _saveToFile(self): try: - while True: - if not self._workNode and self._topNode: - self._msgLock.acquire() - try: - self._workNode = self._topNode - self._topNode = None - self._botNode = None - finally: - self._msgLock.release() - if self._workNode: - filename = datetime.datetime.now().strftime('log_' + str(os.getpid()) + '_%Y%m%d.log') - try: - with open(os.path.join(self._logDir, filename), 'a') as f: - while self._workNode: - nextNode = self._workNode.nextNode - f.write(self._workNode.msg + '\r\n') - self._workNode = nextNode - except Exception as e: - print(str(e), flush=True) - time.sleep(10) + while not self._terminated: + self._internalSave() + self._cond.acquire() + self._cond.wait(10) + self._cond.release() + self._internalSave() except: traceback.print_exec() + def _internalSave(self): + if not self._workNode and self._topNode: + self._msgLock.acquire() + try: + self._workNode = self._topNode + self._topNode = None + self._botNode = None + finally: + self._msgLock.release() + if self._workNode: + filename = datetime.datetime.now().strftime('log_' + str(os.getpid()) + '_%Y%m%d.log') + try: + with open(os.path.join(self._logDir, filename), 'a') as f: + while self._workNode: + nextNode = self._workNode.nextNode + f.write(self._workNode.msg + '\r\n') + self._workNode = nextNode + except Exception as e: + print(str(e), flush=True) + def instance(): return _UdpLog.instance()