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):
tornado.ioloop.IOLoop.current().start() if self._listened:
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,28 +77,34 @@ class _UdpLog:
def _saveToFile(self): def _saveToFile(self):
try: try:
while True: while not self._terminated:
if not self._workNode and self._topNode: self._internalSave()
self._msgLock.acquire() self._cond.acquire()
try: self._cond.wait(10)
self._workNode = self._topNode self._cond.release()
self._topNode = None self._internalSave()
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)
except: except:
traceback.print_exec() 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(): def instance():
return _UdpLog.instance() return _UdpLog.instance()