day⑩:事件驱动网络框架

事件驱动python


例子:app

farm.pyspa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
'''
简而言之,事件驱动分为二个部分:第一,注册事件;第二,触发事件。
'''
 
event_list = []
 
def run():
     for event in event_list:
         obj = event()
         obj.execute()
 
class BaseHandler( object ):
     '''
     用户必须继承该类,从而规范全部类的方法(相似于接口的功能)
     '''
     def excute( self ):
         raise Exception( 'you must overwite execute' )


r​un_farm.pycode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
 
 




'''1.继承类BaseHandler2.必须类里重写execute3.注册到event_list4.执行run方法'''importfarmclassMyHander(farm.BaseHandler):    defexecute(self):        print('重写execute')farm.event_list.append(MyHander)farm.run()





相关文章
相关标签/搜索