transitions 具有有序转换以及 queued 转换,这可能是您正在寻找的。基本技巧是在处理相同的事件时调用相同的事件(使用finalize)。即使转换未成功,传递给finalize 的回调也会被处理(并非所有conditions 都返回True)。使用queued,transitions 不会立即处理事件,而是在当前处理的事件之后立即处理,这将防止大规模递归。
from transitions import Machine
import time
class Model(object):
# initialise counter and counter limit
def __init__(self):
self.counter = 0
self.limit = 5
# will be called in every cycle and increase the counter
def increase_counter(self):
self.counter += 1
print("Counter increased to ", self.counter)
time.sleep(0.5)
# will be called whenever a new state has been entered
def reset_counter(self):
self.counter = 0
print("Counter reset; Current state is ", model.state)
# this function returns whether the limit has already been reached
def limit_reached(self):
return self.counter >= self.limit
# initialising the previously defined model
model = Model()
# creating some state names
states = ['A', 'B', 'C', 'D']
# configuring the state machine:
# pass the model (for callbacks), pass the state names,
# disable auto_transitions since we will not need them
# set the initial state to 'A' and call a (currently) undefined
# model function 'next_state' after EVERY triggered event.
# 'queued' means that every transition is finished before the next event is handled
machine = Machine(model, states=states, auto_transitions=False,
queued=True, initial='A', finalize_event='next_state')
# now add ordered transitions:
# Depending on the order of the passed state names,
# create transitions from each state 'n' to state 'n+1' called 'next_state'.
# 'prepare' each transition attempt by increasing the counter
# afterwards check the 'conditions' (is the counter limit reached)
# if all callbacks in 'conditions' return True, the transition
# is conducted and callbacks in 'after' are processed (counter reset)
machine.add_ordered_transitions(prepare='increase_counter', conditions='limit_reached',
after='reset_counter', trigger='next_state')
# model will go into an infinite loop; can be triggered in a thread
model.next_state()
您可以尝试减少increase_counter 中的睡眠计时器,以检查您是否会遇到递归错误(您不应该)。如果您将queued=False 设置为标准行为,您将或多或少立即遇到递归错误,因为所有机器触发器都会立即处理。