合并1.6.1版本的event Engine
This commit is contained in:
parent
af2c382132
commit
1a83b9b2c7
@ -4,6 +4,7 @@
|
||||
from Queue import Queue, Empty
|
||||
from threading import Thread
|
||||
from time import sleep
|
||||
from collections import defaultdict
|
||||
|
||||
# 第三方模块
|
||||
from PyQt4.QtCore import QTimer
|
||||
@ -16,7 +17,6 @@ from eventType import *
|
||||
class EventEngine(object):
|
||||
"""
|
||||
事件驱动引擎
|
||||
|
||||
事件驱动引擎中所有的变量都设置为了私有,这是为了防止不小心
|
||||
从外部修改了这些变量的值或状态,导致bug。
|
||||
|
||||
@ -68,7 +68,10 @@ class EventEngine(object):
|
||||
|
||||
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
||||
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
||||
self.__handlers = {}
|
||||
self.__handlers = defaultdict(list)
|
||||
|
||||
# __generalHandlers是一个列表,用来保存通用回调函数(所有事件均调用)
|
||||
self.__generalHandlers = []
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __run(self):
|
||||
@ -90,7 +93,11 @@ class EventEngine(object):
|
||||
|
||||
# 以上语句为Python列表解析方式的写法,对应的常规循环写法为:
|
||||
#for handler in self.__handlers[event.type_]:
|
||||
#handler(event)
|
||||
#handler(event)
|
||||
|
||||
# 调用通用处理函数进行处理
|
||||
if self.__generalHandlers:
|
||||
[handler(event) for handler in self.__generalHandlers]
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __onTimer(self):
|
||||
@ -102,8 +109,11 @@ class EventEngine(object):
|
||||
self.put(event)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def start(self):
|
||||
"""引擎启动"""
|
||||
def start(self, timer=True):
|
||||
"""
|
||||
引擎启动
|
||||
timer:是否要启动计时器
|
||||
"""
|
||||
# 将引擎设为启动
|
||||
self.__active = True
|
||||
|
||||
@ -111,7 +121,8 @@ class EventEngine(object):
|
||||
self.__thread.start()
|
||||
|
||||
# 启动计时器,计时器事件间隔默认设定为1秒
|
||||
self.__timer.start(1000)
|
||||
if timer:
|
||||
self.__timer.start(1000)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def stop(self):
|
||||
@ -128,12 +139,8 @@ class EventEngine(object):
|
||||
#----------------------------------------------------------------------
|
||||
def register(self, type_, handler):
|
||||
"""注册事件处理函数监听"""
|
||||
# 尝试获取该事件类型对应的处理函数列表,若无则创建
|
||||
try:
|
||||
handlerList = self.__handlers[type_]
|
||||
except KeyError:
|
||||
handlerList = []
|
||||
self.__handlers[type_] = handlerList
|
||||
# 尝试获取该事件类型对应的处理函数列表,若无defaultDict会自动创建新的list
|
||||
handlerList = self.__handlers[type_]
|
||||
|
||||
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
||||
if handler not in handlerList:
|
||||
@ -143,24 +150,34 @@ class EventEngine(object):
|
||||
def unregister(self, type_, handler):
|
||||
"""注销事件处理函数监听"""
|
||||
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
||||
try:
|
||||
handlerList = self.__handlers[type_]
|
||||
handlerList = self.__handlers[type_]
|
||||
|
||||
# 如果该函数存在于列表中,则移除
|
||||
if handler in handlerList:
|
||||
handlerList.remove(handler)
|
||||
# 如果该函数存在于列表中,则移除
|
||||
if handler in handlerList:
|
||||
handlerList.remove(handler)
|
||||
|
||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
||||
if not handlerList:
|
||||
del self.__handlers[type_]
|
||||
|
||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
||||
if not handlerList:
|
||||
del self.__handlers[type_]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def put(self, event):
|
||||
"""向事件队列中存入事件"""
|
||||
self.__queue.put(event)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def registerGeneralHandler(self, handler):
|
||||
"""注册通用事件处理函数监听"""
|
||||
if handler not in self.__generalHandlers:
|
||||
self.__generalHandlers.append(handler)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def unregisterGeneralHandler(self, handler):
|
||||
"""注销通用事件处理函数监听"""
|
||||
if handler in self.__generalHandlers:
|
||||
self.__generalHandlers.remove(handler)
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
class EventEngine2(object):
|
||||
@ -187,7 +204,10 @@ class EventEngine2(object):
|
||||
|
||||
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
||||
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
||||
self.__handlers = {}
|
||||
self.__handlers = defaultdict(list)
|
||||
|
||||
# __generalHandlers是一个列表,用来保存通用回调函数(所有事件均调用)
|
||||
self.__generalHandlers = []
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __run(self):
|
||||
@ -209,7 +229,11 @@ class EventEngine2(object):
|
||||
|
||||
# 以上语句为Python列表解析方式的写法,对应的常规循环写法为:
|
||||
#for handler in self.__handlers[event.type_]:
|
||||
#handler(event)
|
||||
#handler(event)
|
||||
|
||||
# 调用通用处理函数进行处理
|
||||
if self.__generalHandlers:
|
||||
[handler(event) for handler in self.__generalHandlers]
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __runTimer(self):
|
||||
@ -225,8 +249,11 @@ class EventEngine2(object):
|
||||
sleep(self.__timerSleep)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def start(self):
|
||||
"""引擎启动"""
|
||||
def start(self, timer=True):
|
||||
"""
|
||||
引擎启动
|
||||
timer:是否要启动计时器
|
||||
"""
|
||||
# 将引擎设为启动
|
||||
self.__active = True
|
||||
|
||||
@ -234,8 +261,9 @@ class EventEngine2(object):
|
||||
self.__thread.start()
|
||||
|
||||
# 启动计时器,计时器事件间隔默认设定为1秒
|
||||
self.__timerActive = True
|
||||
self.__timer.start()
|
||||
if timer:
|
||||
self.__timerActive = True
|
||||
self.__timer.start()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def stop(self):
|
||||
@ -253,12 +281,8 @@ class EventEngine2(object):
|
||||
#----------------------------------------------------------------------
|
||||
def register(self, type_, handler):
|
||||
"""注册事件处理函数监听"""
|
||||
# 尝试获取该事件类型对应的处理函数列表,若无则创建
|
||||
try:
|
||||
handlerList = self.__handlers[type_]
|
||||
except KeyError:
|
||||
handlerList = []
|
||||
self.__handlers[type_] = handlerList
|
||||
# 尝试获取该事件类型对应的处理函数列表,若无defaultDict会自动创建新的list
|
||||
handlerList = self.__handlers[type_]
|
||||
|
||||
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
||||
if handler not in handlerList:
|
||||
@ -268,24 +292,33 @@ class EventEngine2(object):
|
||||
def unregister(self, type_, handler):
|
||||
"""注销事件处理函数监听"""
|
||||
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
||||
try:
|
||||
handlerList = self.__handlers[type_]
|
||||
handlerList = self.__handlers[type_]
|
||||
|
||||
# 如果该函数存在于列表中,则移除
|
||||
if handler in handlerList:
|
||||
handlerList.remove(handler)
|
||||
# 如果该函数存在于列表中,则移除
|
||||
if handler in handlerList:
|
||||
handlerList.remove(handler)
|
||||
|
||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
||||
if not handlerList:
|
||||
del self.__handlers[type_]
|
||||
except KeyError:
|
||||
pass
|
||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
||||
if not handlerList:
|
||||
del self.__handlers[type_]
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def put(self, event):
|
||||
"""向事件队列中存入事件"""
|
||||
self.__queue.put(event)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def registerGeneralHandler(self, handler):
|
||||
"""注册通用事件处理函数监听"""
|
||||
if handler not in self.__generalHandlers:
|
||||
self.__generalHandlers.append(handler)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def unregisterGeneralHandler(self, handler):
|
||||
"""注销通用事件处理函数监听"""
|
||||
if handler in self.__generalHandlers:
|
||||
self.__generalHandlers.remove(handler)
|
||||
|
||||
|
||||
########################################################################
|
||||
class Event:
|
||||
@ -311,7 +344,8 @@ def test():
|
||||
app = QCoreApplication(sys.argv)
|
||||
|
||||
ee = EventEngine2()
|
||||
ee.register(EVENT_TIMER, simpletest)
|
||||
#ee.register(EVENT_TIMER, simpletest)
|
||||
ee.registerGeneralHandler(simpletest)
|
||||
ee.start()
|
||||
|
||||
app.exec_()
|
||||
|
Loading…
Reference in New Issue
Block a user