gpt4 book ai didi

python - 在 "on_enter"回调中更改状态时的回调顺序

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:09 25 4
gpt4 key购买 nike

我使用状态机 python 实现转换

当我尝试在 on_enter 回调中直接更改机器状态时,回调调用的顺序不是我期望的顺序。

请在下面找到发生问题的最小可运行代码:

# coding: utf8

"""Minimal script."""

from transitions.extensions import GraphMachine as Machine

class MyStateMachine(object):
"""My state machine"""

def __init__(self):
"""Initialization."""
super(MyStateMachine, self).__init__()

states = ["state_a", "state_b"]
transitions = [
{
"trigger": "go_b",
"source": "state_a",
"dest": "state_b",
"before": "before",
"after": "after",
},
{
"trigger": "go_a",
"source": "state_b",
"dest": "state_a",
"before": "before",
"after": "after",
},
]

self.__machine = Machine(self, states=states, transitions=transitions,
initial="state_a")

def before(self):
"""Before transition."""
print "before transition"

def after(self):
"""After transition."""
print "after transition - current state:", self.state

def on_enter_state_a(self):
"""When entering in state A."""
print "enter state A"

def on_exit_state_a(self):
"""When exiting state A."""
print "exit state A"

def on_enter_state_b(self):
"""When entering in state A."""
print "enter state B"
self.go_a()

def on_exit_state_b(self):
"""When exiting state A."""
print "exit state B"


def main():
"""Main function."""
machine = MyStateMachine()
machine.go_b()

if __name__ == '__main__':
main()

预期输出:

before transition
exit state A
enter state B
after transition - current state: state_b
before transition
exit state B
enter state A
after transition - current state: state_a

观察到的输出:

before transition
exit state A
enter state B
before transition
exit state B
enter state A
after transition - current state: state_a
after transition - current state: state_a

可以将其视为错误吗?如果不是,我怎样才能得到预期的输出?

最佳答案

答案:将机器关键字 queued 设置为 True

来自转换Readme :

The default behaviour in Transitions is to process events instantly. This means events within an on_enter method will be processed before callbacks bound to after are called [...] If queued processing is enabled, a transition will be finished before the next transition is triggered: machine = Machine(states=states, queued=True) [...]

您需要用queued=True 初始化Machine。上面提到的部分涵盖了两种情况下回调的执行顺序,以及启用排队时 Machine 行为的变化。我将 queued 作为关键字添加到您提供的示例中以说明该过程:

from transitions.extensions import GraphMachine as Machine


class MyStateMachine(object):
"""My state machine"""
# added 'queued' to constructor of custom class...
def __init__(self, queued=False):
"""Initialization."""
super(MyStateMachine, self).__init__()

states = ["state_a", "state_b"]
transitions = [
{
"trigger": "go_b",
"source": "state_a",
"dest": "state_b",
"before": "before",
"after": "after",
},
{
"trigger": "go_a",
"source": "state_b",
"dest": "state_a",
"before": "before",
"after": "after",
},
]
# ... to pass the value to 'Machine'
self.__machine = Machine(self, states=states, transitions=transitions,
initial="state_a", queued=queued)

def before(self):
print "before transition"

def after(self):
print "after transition - current state:", self.state

def on_enter_state_a(self):
print "enter state A"

def on_exit_state_a(self):
print "exit state A"

def on_enter_state_b(self):
print "enter state B"
self.go_a()

def on_exit_state_b(self):
"""When exiting state A."""
print "exit state B"


def main():
print "---- Standard behaviour ----"

machine = MyStateMachine()
machine.go_b()

print "---- Now queued ----"

queued_machine = MyStateMachine(queued=True)
queued_machine.go_b()

if __name__ == '__main__':
main()

输出:

---- Standard behaviour ----
before transition
exit state A
enter state B
before transition
exit state B
enter state A
after transition - current state: state_a
after transition - current state: state_a
---- Now queued ----
before transition
exit state A
enter state B
after transition - current state: state_b
before transition
exit state B
enter state A
after transition - current state: state_a

如果您关心:转换支持日志记录

这样你就不需要用打印语句来打乱你的代码了:

from transitions.extensions import GraphMachine as Machine
import logging


class Model(object):

def before(self):
pass

def after(self):
pass

logging.basicConfig(level=logging.DEBUG)

model = Model()
machine = Machine(model, states=['A', 'B'], before_state_change='before',
after_state_change='after', initial='A')
model.to_B()

输出:

DEBUG:transitions.core:Initiating transition from state A to state B...
DEBUG:transitions.core:Executed callback 'before' before transition.
DEBUG:transitions.core:Exiting state A. Processing callbacks...
INFO:transitions.core:Exited state A
DEBUG:transitions.core:Entering state B. Processing callbacks...
INFO:transitions.core:Entered state B
DEBUG:transitions.core:Executed callback 'after' after transition.

关于python - 在 "on_enter"回调中更改状态时的回调顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com