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 回调函数都是异步函数。

相关内容

热门资讯

线上(wepoke真的)原来是... 线上(wepoke真的)原来是真的有挂!其实真的有挂(2022已更新)(哔哩哔哩);亲,其实确实真的...
两教程(Wepoke程序)软件... 两教程(Wepoke程序)软件透明挂辅助工具(软件透明挂)透视辅助(2024已更新)(哔哩哔哩);致...
软件(wepoke透明)原来是... 软件(wepoke透明)原来是真的有挂!其实真的有挂(2020已更新)(哔哩哔哩)是一款可以让一直输...
一模拟器(德扑工具)外挂辅助工... 一模拟器(德扑工具)外挂辅助工具(透视)透视辅助(2025已更新)(哔哩哔哩);亲真的是有正版授权,...
系统(aapoker讲解)竟然... 系统(aapoker讲解)竟然真的有挂!其实真的有挂(2021已更新)(哔哩哔哩);aapoker讲...
6系统(aapoker下载)外... 6系统(aapoker下载)外挂辅助工具(辅助挂)透视辅助(2023已更新)(哔哩哔哩)aapoke...
智能(德扑之星刷数据)果真真的... 智能(德扑之星刷数据)果真真的有挂!原来真的有挂(2025已更新)(哔哩哔哩);《WPK辅助透视》‌...
1机器人(德州nzt软件)软件... 1机器人(德州nzt软件)软件透明挂辅助软件(透视)透视辅助(2022已更新)(哔哩哔哩);人气非常...
ai代打(德扑之星决策)确实是... ai代打(德扑之星决策)确实是真的有挂!原来真的有挂(2020已更新)(哔哩哔哩);科技详细教程小薇...
第8透明(wepoke数据)外... 第8透明(wepoke数据)外挂透明挂辅助神器(辅助挂)透视辅助(2023已更新)(哔哩哔哩);原来...