Note
Go to the end to download the full example code.
Async without external loop¶
Demonstrates that the state machine can have async callbacks even if the calling context is synchronous.
from statemachine import State
from statemachine import StateChart
class AsyncStateMachine(StateChart):
initial = State("Initial", initial=True)
processing = State()
final = State("Final", final=True)
start = initial.to(processing)
finish = processing.to(final)
async def on_start(self):
return "starting"
async def on_finish(self):
return "finishing"
Executing¶
def sync_main():
sm = AsyncStateMachine()
result = sm.start()
print(f"Start result is {result}")
result = sm.send("finish")
print(f"Finish result is {result}")
print(list(sm.configuration))
assert sm.final in sm.configuration
if __name__ == "__main__":
sync_main()
Start result is starting
Finish result is finishing
[State('Final', id='final', value='final', initial=False, final=True, parallel=False)]