-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathrecursive_event_machine.py
More file actions
40 lines (28 loc) · 913 Bytes
/
recursive_event_machine.py
File metadata and controls
40 lines (28 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Looping state machine
=====================
This example demonstrates that you can call an event as a side-effect of another event.
The event will be put on an internal queue and processed in the same loop after the previous event
in the queue is processed.
"""
from statemachine import State
from statemachine import StateChart
class MyStateMachine(StateChart):
catch_errors_as_events = False
startup = State(initial=True)
test = State()
counter = 0
do_startup = startup.to(test, after="do_test")
do_test = test.to.itself(after="do_test")
def on_enter_state(self, target, event):
self.counter += 1
print(f"{self.counter:>3}: Entering {target} from {event}")
if self.counter >= 5:
raise StopIteration
# %%
# Let's create an instance and test the machine.
sm = MyStateMachine()
try:
sm.do_startup()
except StopIteration:
pass