-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State timeouts? #198
Comments
Hi @Eric24, I also use timeouts in my projects but in my oppinion this should be handled by the model or the specific state. Let's see if others may have another view on this. Until then, a straight forward way to do it in the model could look like this: from threading import Thread
import time
from transitions import Machine
class Timeout(Thread):
def __init__(self, func, timeout):
super(Timeout, self).__init__()
self.func = func
self.timeout = timeout
self.canceled = False
self.start()
def run(self):
time.sleep(self.timeout/1000.0)
if not self.canceled:
self.func()
class Model:
def __init__(self):
self.timer = None
def on_enter_B(self):
self.timer = Timeout(self.timeout, 1000)
def on_exit_B(self):
self.timer.canceled = True
model = Model()
transitions = [['timeout', 'B', 'C']]
machine = Machine(model, states=['A', 'B', 'C'], transitions=transitions, initial='A')
model.to_B()
print(model.state) # >>> B
time.sleep(1.1)
print(model.state) # >>> C or as part of a subclassed class TimeoutState(State):
def __init__(self, timeout):
self.timeout = timeout
b = TimeoutState('B', enter='set_timeout')
m = Machine(model, states=['A', b, 'C']) ... |
Yes, that's essentially my current approach. I'm curious as to why you feel that this shouldn't be part of the state machine "internals"? To me, it seems like "core functionality", and as such, why trouble the developer to handle their own timeouts explicitly? For my purposes, having 30 or more states, most with timeouts, it seems like a lot of unnecessary boilerplate. |
If you subclass class TimeoutState(State):
def __init__(self, timeout=None, *args, **kwargs):
self.timeout = timeout
super(TimeoutState, self).__init__(*args, **kwargs)
def enter(self, event_data):
# initialise timeout here
class TimeoutMachine(Machine):
def _create_state(self, *args, **kwargs):
return TimeoutState(*args, **kwargs)
states = [{'name': 'A', 'timeout': 1000},
{'name': 'B', 'timeout': 2000'},...]
machine = TimeoutMachine(states=states)
I do not consider timeouts as a |
Very good idea! -> upvote |
Me again guys :) Can't get run the above code. What the ... ;) Thanks! |
My next try to implement default timouts using Please see the issue on stack http://stackoverflow.com/questions/43937365/setting-and-canceling-of-predefined-timeouts-for-hierarchical-state-machine-fail Thnx! |
I got here looking for the same thing. Built-in support for timeouts would make for less overall LOC in the client apps. |
Just to add my $.02, I ended up just using threading.Timers:
|
We plan to ship timeouts with transitions 0.6.0. See #229 or the whole |
What restrictions does this place on code that uses timeouts? Presumably it needs threads, which precludes the ability to use joblib in the same process (with python 2.x only anyway) I've already solved this problem by separating my concurrent code in a different process, but I'm curious if the implementation has the same restrictions as I ran into. |
Correct. It actually uses
Yeah, it does. I am not aware of any convenient solution which allows timeouts without threads 🤔. I could think of two threadless solutions but both have major issues: a) Using @bedge: What's your lesson learned for timeouts/threading? Should |
I believe that the threading restrictions are mainly python <3 only and that >3 has fixed many of the threading problems and inconsistencies. |
well, that should not be the case except joblib already complains when the threading module is imported. Timers are only created when Timeout is explicitly used. |
While it's unfortunate, it's not really transitions' fault that the python 2.x threading has issues. |
agree, from what I heard in my department so far, one should use a single threaded state machine where ever possible. Closing this issue now since |
It's a very interesting project with a lot of rich functionality. One of the the things I was looking for in an FSM was the ability to automatically transition to a specified state after a period of time in the current state. For example, the FMS is in "tank-filling" state, waiting for an external sensor to tell me the tank is full and move on to "tank-full" state, but if that external event doesn't happen within 30 seconds, I instead want to move to "tank-fill-error" state. Of course I could implement something like this with an external timer that fires after 30 seconds and triggers a transition from "tank-filling" to "tank-fill-error", but it seems like the use-case for this sort of thing is common enough to be built in--essentially, for any state transition, the ability to pass an optional "timeout state" and timeout value. I'd love to hear your thoughts on this.
The text was updated successfully, but these errors were encountered: