day⑩:事件驱动网络框架

事件驱动


例子:

farm.py

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.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'''
1.继承类BaseHandler
2.必须类里重写execute
3.注册到event_list
4.执行run方法
'''
 
import farm
 
class MyHander(farm.BaseHandler):
    def execute(self):
        print('重写execute')
 
 
farm.event_list.append(MyHander)
farm.run()











原文地址:https://www.cnblogs.com/binhy0428/p/5545070.html