AsyncGraphMachineusedwithasyncon_entercallbacks
创始人
2024-09-21 03:01:15
0

AsyncGraphMachine 是一个状态机库,它支持异步的 on_enter 回调函数。在使用 AsyncGraphMachine 时,需要确保所有的 on_enter 回调函数都是异步函数,否则可能会出现“AsyncGraphMachine used with async on_enter callbacks”错误。为解决该问题,需要确认 on_enter 回调函数均为异步函数,示例如下:

from transitions_async import AsyncGraphMachine
import asyncio

# 定义状态机
class Matter(object):
    # 状态_ENUM
    states = ['solid', 'liquid', 'gas', 'plasma']

    # 转换_definitions
    transitions = [
        {'trigger': 'melt', 'source': 'solid', 'dest': 'liquid'},
        {'trigger': 'evaporate', 'source': 'liquid', 'dest': 'gas'},
        {'trigger': 'ionize', 'source': 'gas', 'dest': 'plasma'},
        {'trigger': 'condense', 'source': 'gas', 'dest': 'liquid'},
        {'trigger': 'freeze', 'source': 'liquid', 'dest': 'solid'}
    ]

    # 定义异步 on_enter 回调函数
    async def on_enter_gas(self):
        print("Gas: Can you see me?")

    async def on_enter_solid(self):
        print("Solid: Hi, I am here.")

# 创建状态机
machine = AsyncGraphMachine(model=Matter(), states=Matter.states, transitions=Matter.transitions, initial='solid')

# 运行状态机
async def run():
    await asyncio.sleep(0) # 等待一段时间后执行转换
    await machine.melt()
    await machine.evaporate()
    await machine.ionize()
    await machine.condense()
    await machine.freeze()

# 执行状态机
asyncio.run(run())

在上述示例中,异步的 on_enter 回调函数分别被定义为 on_enter_gas 和 on_enter_solid,确保了所有的 on_enter 回调函数都是异步函数。

相关内容

热门资讯

第十分钟辅助!wepoker私... 第十分钟辅助!wepoker私人局俱乐部怎么进,wepoker透视有吗,练习教程(有挂教程)wepo...
6分钟辅助!pokerrrr2... 6分钟辅助!pokerrrr2辅助,哈糖大菠萝免费辅助器,举措教程(有挂猫腻)1)哈糖大菠萝免费辅助...
五分钟辅助!wepoker底牌... 五分钟辅助!wepoker底牌透视,淘宝买wepoker透视有用吗,教程书教程(揭秘有挂)所有人都在...
9分钟辅助!wepoker轻量... 9分钟辅助!wepoker轻量版有透视吗,wepoker祈福有用吗,总结教程(有挂规律)1、玩家可以...
七分钟辅助!wpk辅助器,we... 七分钟辅助!wpk辅助器,wepoker有辅助功能吗,方针教程(有挂详细)1、七分钟辅助!wpk辅助...
6分钟辅助!来玩app破解版,... 6分钟辅助!来玩app破解版,wepoker辅助器免费,方针教程(有挂细节)1、在wepoker辅助...
五分钟辅助!模拟器打开hhpo... 五分钟辅助!模拟器打开hhpoker,hhpoker辅助器,教程书教程(竟然有挂)1、下载好模拟器打...
六分钟辅助!来玩app破解,德... 六分钟辅助!来玩app破解,德州局脚本,方式教程(有挂教程)1、进入到德州局脚本是否有挂之后,能看到...
第三分钟辅助!约局吧是否有挂,... 第三分钟辅助!约局吧是否有挂,wepoker黑侠破解,经验教程(有挂解密)该软件可以轻松地帮助玩家将...
6分钟辅助!aapoker免费... 6分钟辅助!aapoker免费透视脚本,德州透视是真的假的,指引教程(有挂教学)德州透视是真的假的辅...