Python Simulated Event trigger mechanism and python event trigger mechanism
EventManager. py
1 #-*-encoding: UTF-8-*-2 3 # System Module 4 from queue import Queue, Empty 5 from threading import * 6 7 8 class EventManager: 9 def _ init _ (self): 10 "initialize event manager" 11 # list of event Objects 12 self. _ eventQueue = Queue () 13 # Switch event manager 14 self. _ active = False15 # event processing thread 16 self. _ thread = Thread (target = self. _ Run) 17 18 # Here _ handlers is a dictionary used to save the corresponding Event Response Function 19 # the value of each key is a list, the list stores the response function listening for this event, one to more than 20 self. _ handlers ={}# {event type: [event processing method]} 21 22 def _ Run (self ): 23 "engine running" 24 while self. _ active = True: 25 try: 26 # Set the blocking time of the event to 1 second 27 event = self. _ eventQueue. get (block = True, timeout = 1) 28 self. _ EventProcess (event) 29 blocks t Empty: 30 pass31 32 def _ EventProcess (self, event ): 33 "Processing event" 34 # Check whether the processing function listening to this event exists 35 if event. type _ in self. _ handlers: 36 # If yes, the event is passed to the processing function in order to execute 37 for handler in self. _ handlers [event. type _]: 38 handler (event) 39 40 def Start (self): 41 "Start" "42 # Set event manager to Start 43 self. _ active = True44 # Start the event processing thread 45 self. _ thread. start () 46 47 def Stop (self): 48 "Stop" "49 # Set event manager to Stop 50 self. _ active = False51 # Wait for the event processing thread to exit 52 self. _ thread. join () 53 54 def AddEventListener (self, type _, handler ): 55 "binding event and listener handler functions" "56 # try to obtain the handler list corresponding to this event type. If not, create 57 try: 58 handlerList = self. _ handlers [type _] 59 handle T KeyError: 60 handlerList = [] 61 62 self. _ handlers [type _] = handlerList63 # if the processor to be registered is not in the processor list of the event, register the event 64 if handler not in handlerList: 65 handlerList. append (handler) 66 67 def RemoveEventListener (self, type _, handler ): 68 "handler for removing listeners" 69 # The Reader tries to implement 70 71 def SendEvent (self, event): 72 "Send events, store the event "73 self. _ eventQueue. put (event) 74 75 "Event object" 76 class event: 77 def _ init _ (self, type _ = None): 78 self. type _ = type _ # Event type 79 self. dict = {}# the dictionary is used to save specific event data.
Test. py
1 #-*-encoding: UTF-8-*-2 3 from threading import * 4 from EventManager import * 5 import time 6 7 # event name new article 8 EVENT_ARTICAL = "Event_Artical" 9 10 11 # event source public account 12 class PublicAccounts: 13 def _ init _ (self, eventManager): 14 self. _ eventManager = eventManager15 16 def WriteNewArtical (self): 17 # event object, written new article 18 Event = event (type _ = EVENT_ARTICAL) 19 event. dict ["artical"] = U' how to write more elegant code \ n' 20 # Send event 21 self. _ eventManager. sendEvent (event) 22 print (u'send new article with public account ') 23 24 25 # Listener subscriber 26 class Listener: 27 def _ init _ (self, username ): 28 self. _ username = username29 30 # listener's processing function read Article 31 def ReadArtical (self, event): 32 print (u '% s received new article' % self. _ username) 33 print (U' reading new article content: % s' % event. dict ["artical"]) 34 35 36 "test function" "37 def test (): 38 listner1 = Listener (" thinkroom ") # subscriber 139 listner2 = Listener ("steve") # subscriber 240 41 eventManager = EventManager () 42 43 # bind event and Listener response functions (new article) 44 eventManager. addEventListener (EVENT_ARTICAL, listner1.ReadArtical) 45 eventManager. addEventListener (EVENT_ARTICAL, listner2.ReadArtical) 46 eventManager. start () 47 48 publicAcc = PublicAccounts (eventManager) 49 while True: 50 publicAcc. writeNewArtical () 51 time. sleep (2) 52 53 if _ name _ = '_ main _': 54 test ()