This commit is contained in:
aozhiwei 2020-01-10 19:16:01 +08:00
parent 24776f8e82
commit 07f321e383
3 changed files with 46 additions and 26 deletions

View File

@ -23,6 +23,7 @@ class _App:
return cls._instance return cls._instance
def __init__(self): def __init__(self):
self._listened = False
self._requestHandler = {} self._requestHandler = {}
self._app = tornado.web.Application([ self._app = tornado.web.Application([
(r"/webapp/index[\.]php", _mainHandler), (r"/webapp/index[\.]php", _mainHandler),
@ -34,10 +35,14 @@ class _App:
f7.udplog.setLogDirAndCreate(log_dir) f7.udplog.setLogDirAndCreate(log_dir)
def start(self): def start(self):
if self._listened:
tornado.ioloop.IOLoop.current().start() tornado.ioloop.IOLoop.current().start()
f7.timer.unInit()
f7.udplog.unInit()
def listen(self, port): def listen(self, port):
self._app.listen(port) self._app.listen(port)
self._listened = True
def callAt(self, when, callback): def callAt(self, when, callback):
tornado.ioloop.IOLoop.current().call_at(when, callback) tornado.ioloop.IOLoop.current().call_at(when, callback)

View File

@ -15,6 +15,9 @@ class _Timer:
def __init__(self): def __init__(self):
pass pass
def unInit(self):
pass
def callAt(self, when, callback): def callAt(self, when, callback):
f7.app.callAt(when, callback) f7.app.callAt(when, callback)

View File

@ -4,7 +4,6 @@ import os
import time import time
import datetime import datetime
import traceback import traceback
import _thread
import threading import threading
import q7 import q7
@ -28,19 +27,26 @@ class _UdpLog:
self._topNode = None self._topNode = None
self._botNode = None self._botNode = None
self._workNode = None self._workNode = None
self._terminated = False
self._cond = threading.Condition()
self._msgLock = threading.Lock() self._msgLock = threading.Lock()
self._workerThread = threading.Thread(target = self._saveToFile, args = ())
def init(self): 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): def setLogDirAndCreate(self, logdir):
if not os.path.exists(logdir): if not os.path.exists(logdir):
os.makedirs(logdir) os.makedirs(logdir)
self._logDir = logdir self._logDir = logdir
def unInit(self):
pass
def info(self, msg): def info(self, msg):
self._addLog(str(datetime.datetime.now()) + '[INFO]' + str(msg)) self._addLog(str(datetime.datetime.now()) + '[INFO]' + str(msg))
@ -71,7 +77,16 @@ class _UdpLog:
def _saveToFile(self): def _saveToFile(self):
try: try:
while True: 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: if not self._workNode and self._topNode:
self._msgLock.acquire() self._msgLock.acquire()
try: try:
@ -90,9 +105,6 @@ class _UdpLog:
self._workNode = nextNode self._workNode = nextNode
except Exception as e: except Exception as e:
print(str(e), flush=True) print(str(e), flush=True)
time.sleep(10)
except:
traceback.print_exec()
def instance(): def instance():
return _UdpLog.instance() return _UdpLog.instance()