1
This commit is contained in:
parent
24776f8e82
commit
07f321e383
@ -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):
|
||||
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)
|
||||
|
@ -15,6 +15,9 @@ class _Timer:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def unInit(self):
|
||||
pass
|
||||
|
||||
def callAt(self, when, callback):
|
||||
f7.app.callAt(when, callback)
|
||||
|
||||
|
30
f7/udplog.py
30
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,7 +77,16 @@ class _UdpLog:
|
||||
|
||||
def _saveToFile(self):
|
||||
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:
|
||||
self._msgLock.acquire()
|
||||
try:
|
||||
@ -90,9 +105,6 @@ class _UdpLog:
|
||||
self._workNode = nextNode
|
||||
except Exception as e:
|
||||
print(str(e), flush=True)
|
||||
time.sleep(10)
|
||||
except:
|
||||
traceback.print_exec()
|
||||
|
||||
def instance():
|
||||
return _UdpLog.instance()
|
||||
|
Loading…
x
Reference in New Issue
Block a user