Arpit Bhayani Avatar

Arpit Bhayani

A Computer Science Engineer who has most recently spent 5 years building scalable distributed systems and search engines at Amazon, Practo, and Unacademy.

Arpit Bhayani arpitbhayani.me

Building finite state machines with Python Coroutines

An excellent, deep primer on both FSMs and using Coroutines in Python.

Even though this may not be the most efficient way to implement and build FSM but it is the most intuitive way indeed. The edges and state transitions, translate well into if and elif statements or the decision functions, while each state is being modeled as an independent coroutine and we still do things in a sequential manner. The entire execution is like a relay race where the baton of execution is being passed from one coroutine to another.

0:00 / 0:00