添加im消息
This commit is contained in:
parent
07f321e383
commit
28da41e6f7
41
f7/app.py
41
f7/app.py
@ -4,6 +4,7 @@
|
||||
import f7
|
||||
|
||||
import json
|
||||
import threading
|
||||
import traceback
|
||||
import tornado.web
|
||||
import tornado.ioloop
|
||||
@ -13,6 +14,8 @@ class _mainHandler(tornado.web.RequestHandler):
|
||||
def get(self):
|
||||
instance()._dispatchRequest(self)
|
||||
|
||||
_IM_DoneCallback = 100
|
||||
|
||||
class _App:
|
||||
_instance = None
|
||||
|
||||
@ -29,6 +32,9 @@ class _App:
|
||||
(r"/webapp/index[\.]php", _mainHandler),
|
||||
])
|
||||
self.registerHandler('Ops', 'selfChecking', self.__selfChecking)
|
||||
self._immsgLock = threading.Lock()
|
||||
self._immsgList = []
|
||||
tornado.ioloop.IOLoop.PeriodicCallback(self._processIMMsg, 100)
|
||||
|
||||
def init(self, log_dir):
|
||||
f7.udplog.init()
|
||||
@ -78,5 +84,40 @@ class _App:
|
||||
'max_rundelay': 10
|
||||
})
|
||||
|
||||
def _processIMMsg(self):
|
||||
self._immsgLock.acquire()
|
||||
try:
|
||||
if len(self._immsgList) > 0:
|
||||
for node in self._immsgList:
|
||||
self._dispatchIMMsg(node[0], node[1])
|
||||
finally:
|
||||
self._immsgList = []
|
||||
self._immsgLock.release()
|
||||
|
||||
def _dispatchIMMsg(self, msgid, callback):
|
||||
try:
|
||||
if msgid == _IM_DoneCallback:
|
||||
self._IMDoneCallback(callback)
|
||||
except Exception as e:
|
||||
f7.udplog.error('_dispatchIMMsg ' + str(e))
|
||||
|
||||
def _addIMMsg(self, msgid, callback):
|
||||
self._immsgLock.acquire()
|
||||
try:
|
||||
self._immsgList.append((msgid, callback))
|
||||
finally:
|
||||
self._immsgLock.release()
|
||||
|
||||
def _IMDoneCallback(self, callback):
|
||||
callback()
|
||||
|
||||
def createAsyncTask(self, done_callback, func, *args):
|
||||
def thread_func(args):
|
||||
func(args)
|
||||
if done_callback:
|
||||
self._addIMMsg(_IM_DoneCallback, done_callback)
|
||||
thread = threading.Thread(target = thread_func, args = args)
|
||||
thread.start()
|
||||
|
||||
def instance():
|
||||
return _App.instance()
|
||||
|
Loading…
x
Reference in New Issue
Block a user