3 ms·
A better title would be: “Person who doesn’t know how to write state machines struggles to write a state machine”. In attempt 2 the old school C way of writing
by seanhunter 8mo ago
A better title would be: “Person who doesn’t know how to write state machines struggles to write a state machine”.
In attempt 2 the old school C way of writing the state machine would work just fine in python, avoid a bunch of the boilerplate and avoid the “state setter needs to know a bunch of stuff” problem. Basically you make the states as a table and put the methods you need in the table so in python a dictionary is convenient. Then you have
> def set_state(new_state):
> state = new_state
> events[new_state].set()
Aaand you’re done. When you add a new state, you add an event corresponding to that state into the events table. If the stuff you would put into a conditional in set_state is more complicated, you could make a state transition method and link to it in the table. Or you could make a nested dict or whatever. It’s not hard, and the fact that the author doesn’t know an idomatic way to write a fsm definitely isn’t something that’s wrong with python’s asyncio and shared state.
In general if you’re writing a state machine and you have a lot of “if curr_state == SOME_STATE” logic, chances are it would be better if you used tables.
- cl3misch 8mo agoIs this being downvoted because of the tone, or because state machines are unpopular/inappropriate in this case? Genuine question, because this feels like a sensible solution to the problem as stated in the article.
- mrkeen 8mo agoIt made no reference to the 'shared' in 'shared state'. No mention of asynchrony, multithreading, or the race condition that TFA encountered.
- seanhunter 8mo agoThe “attempt 2” was literally a state machine implementation which the author rejected because they didn’t know how to do it properly and so did it badly using a bunch of if then else logic.