transitions功能有序的转换以及queued
转换可能是您正在寻找。基本技巧是每当处理完相同的事件时调用相同的事件(使用finalize
)。即使转换未成功,传递给finalize
的回叫也将被处理(并非所有conditions
返回True
)。使用queued
转换不会立即处理事件,而是在当前处理的事件之后立即处理事件,这将防止大量递归。
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
这是标准行为,您将立即或多或少地发生递归错误,因为所有计算机触发器都将立即处理。
我可能会看看这个 –
@ user252046的协同程序递归的问题是什么。你为什么要“没有它”? – Rajez
我不希望程序在达到基本情况后回拨。循环永远持续下去。我知道这可以通过调用函数的主循环来完成,但会造成一大堆混乱。 – user252046