标签:frame back one ide bsp pytho == none except
思路来源: 事件驱动框架。
Part 1. 定义一个框架 event_flow.py (需要再改动)
1 #! /usr/bin/env python 2 3 4 deploy_phase_list = [] 5 6 def deploy(): 7 for phase in deploy_phase_list: 8 phase_obj = phase() 9 phase_obj.deploy() 10 11 12 def rollback(): 13 for phase in reversed(deploy_phase_list): 14 phase_obj = phase() 15 phase_obj.rollback() 16 17 18 class BasePhase(object): 19 def deploy(): 20 raise Exception("deploy() must be overwrite") 21 22 def rollback(): 23 raise Exception("rollback() must be overwrite")
// 此处可以考虑?装饰器,当deploy 顺利完成,才把该类append 到rollback list
Part 2. 使用该框架
#! /usr/bin/env python import event_flow class MyFirstStep(event_flow.BasePhase): def deploy(): print("Do First Step to deploy") def rollback(): print("Rollback First Step") class MySecondStep(event_flow.BasePhase): def deploy(): print("Do Second Step to deploy") def rollback(): print("Nothing to rollback") pass class MyThirdStep(event_flow.BasePhase): def deploy(): print("Do Third Step to deploy") def rollback(): print("Rollback third Step") if __name__ == "__main__": event_flow.deploy_phase_list.append(MyFirstStep, MySecondStep, MyThirdStep) try: event_flow.deploy() exception Exception as e: event_flow.rollback()
但是该方法给可以完成单一工作流的部署以及回滚。后续的这种单一工作流,可以参考这种模式进行开发。
其中各个步骤的返回等数据交互,需要借助第三方(例如数据库等)
标签:frame back one ide bsp pytho == none except
原文地址:https://www.cnblogs.com/shuo-yang0459/p/10121472.html