创建一系列依赖操作,并在其中一个操作失败时回滚所有操作

saga_p的Python项目详细描述


创建一系列依赖操作,并在其中一个操作失败时回滚所有操作。

安装

$ pip install saga_py

用法

简单示例

fromsagaimportSagaBuildercounter1=0counter2=0defincr_counter1(amount):globalcounter1counter1+=amountdefincr_counter2(amount):globalcounter2counter2+=amountdefdecr_counter1(amount):globalcounter1counter1-=amountdefdecr_counter2(amount):globalcounter2counter2-=amountSagaBuilder \
    .create() \
    .action(lambda:incr_counter1(15),lambda:decr_counter1(15)) \
    .action(lambda:incr_counter2(1),lambda:decr_counter2(1)) \
    .action() \
    .build() \
    .execute()# if every action succeeds, the effects of all actions are appliedprint(counter1)# 15print(counter2)# 1

操作失败示例

如果一个操作失败,将运行对所有已执行操作的补偿,并引发一个sagaerror来包装 运行期间遇到的所有异常。

fromsagaimportSagaBuilder,SagaErrorcounter1=0counter2=0defincr_counter1(amount):globalcounter1counter1+=amountdefincr_counter2(amount):globalcounter2counter2+=amountraiseBaseException('some error happened')defdecr_counter1(amount):globalcounter1counter1-=amountdefdecr_counter2(amount):globalcounter2counter2-=amounttry:SagaBuilder \
        .create() \
        .action(lambda:incr_counter1(15),lambda:decr_counter1(15)) \
        .action(lambda:incr_counter2(1),lambda:decr_counter2(1)) \
        .action() \
        .build() \
        .execute()exceptSagaErrorase:print(e)# wraps the BaseException('some error happened')print(counter1)# 0print(counter2)# 0

动作和补偿失败示例

因为action2的补偿失败了,所以从框架的角度来看补偿效果是不确定的, 所有其他补偿都不考虑。

fromsagaimportSagaBuilder,SagaErrorcounter1=0counter2=0defincr_counter1(amount):globalcounter1counter1+=amountdefincr_counter2(amount):globalcounter2counter2+=amountraiseBaseException('some error happened')defdecr_counter1(amount):globalcounter1counter1-=amountdefdecr_counter2(amount):globalcounter2raiseBaseException('compensation also fails')try:SagaBuilder \
        .create() \
        .action(lambda:incr_counter1(15),lambda:decr_counter1(15)) \
        .action(lambda:incr_counter2(1),lambda:decr_counter2(1)) \
        .action() \
        .build() \
        .execute()exceptSagaErrorase:print(e)#print(counter1)# 0print(counter2)# 1

将值从一个操作传递到下一个操作

操作可以返回返回值的dict。 然后,dict作为关键字参数传递给下一个操作,并得到相应的补偿。 补偿之间不能传递任何值。

fromsagaimportSagaBuilder,SagaErrorcounter1=0counter2=0defincr_counter1(amount):globalcounter1counter1+=amountreturn{'counter1_value':counter1}defincr_counter2(counter1_value):globalcounter2counter2+=amountdefdecr_counter1(amount):globalcounter1counter1-=amountdefdecr_counter2(counter1_value):globalcounter2counter2-=amountSagaBuilder \
    .create() \
    .action(lambda:incr_counter1(15),lambda:decr_counter1(15)) \
    .action(incr_counter2,decr_counter2) \
    .action() \
    .build() \
    .execute()print(counter1)# 15print(counter2)# 15

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Grid loadmask(true)在gxt中不起作用   java将字符串索引转换为整数   为什么Java泛型适用于基元数组,但不适用于基元数组?   java如何让Azure应用程序服务Tomcat将所有80/443流量转发到运行在同一应用程序服务上的JBoss   具有多个值的java转换映射到树?   java如何设置SQL server连接URL?   java设置了多个相互独立的JFrame   安全性在JAVA中如何使用RSAPrivateKey的密码短语?   java不能使用比Apache Velocity中更大的条件   如何在Java中打印字符串的所有排列   停止Android Studio自动导入java。sql。日期   对象简单计算器程序java   java如何在安卓中使用“&”符号作为字符串?   java Connect,为我的安卓应用程序从REST API发布和获取数据