question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State feature: Timeout

See original GitHub issue

Adds the ability to timeout an entered state. See: states.py

Keywords

  • timeout (int, optional) – if passed, an entered state will timeout after timeout seconds
  • on_timeout (string/callable, optional) – will be called when timeout time has been reached

Exceptions:

  • will raise an AttributeError when timeout is set but on_timeout is not

Example Usage

from transitions import Machine
from transitions.extensions.states import add_state_features, Timeout

import time


@add_state_features(Timeout)
class CustomMachine(Machine):
    pass


class Model(object):

    def timeout(self):
        print("Timeout!")
        self.to_A()

model = Model()

states = ['A',
          {'name': 'B', 'timeout': 0.3, 'on_timeout': 'timeout'},
          {'name': 'C', 'timeout': 0.9, 'on_timeout': model.timeout}]

m = CustomMachine(model, states=states, initial='A')
model.to_B()
time.sleep(1)  # >>> "Timeout!"
print model.state  # >>>  'A'

Remark

This implementation is not thread-safe! The user may either lock the model or use LockedMachine.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Synsscommented, Jul 5, 2017

Hi, great to see this coming. Just two small things: could you put a comment to #198: I was following #198 and overlooked this thread until yesterday. I would also check the presence of on_timeout with EAFP

index 40e9ce7..1d8a01f 100644
--- a/transitions/extensions/states.py
+++ b/transitions/extensions/states.py
@@ -36,8 +36,9 @@ class Timeout(object):
     def __init__(self, *args, **kwargs):
         self.timeout = kwargs.pop('timeout', 0)
         if self.timeout > 0:
-            self.on_timeout = kwargs.pop('on_timeout', None)
-            if self.on_timeout is None:
+            try:
+                self.on_timeout = kwargs["on_timeout"]
+            except KeyError as exc:
                 raise AttributeError("Timeout state requires 'on_timeout' when timeout is set.")
         self.runner = {}
         super(Timeout, self).__init__(*args, **kwargs)

This way, you avoid the if in the normal case.

0reactions
aleneumcommented, Aug 2, 2017

Have you started working on this?

has been implemented and tested. Hope this enables your workflow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use timeouts to avoid stuck executions - AWS Step Functions
You can set a timeout for your state machine using the TimeoutSeconds field in your Amazon States Language definition. For more information, see...
Read more >
Does AWS Step Functions have a timeout feature?
I want a timeout feature for the entire state machine/ emr cluster as a whole, not for each individual task like in your...
Read more >
HttpSessionState.Timeout Property (System.Web.SessionState)
Gets or sets the amount of time, in minutes, allowed between requests before the session-state provider terminates the session.
Read more >
Step Function State Machine has timed out - Dashbird
One of your state machines timed out because one of its tasks ran longer than the TimeoutSeconds value or failed to send a...
Read more >
Using setTimeout in React components (including hooks)
The setTimeout function accepts two arguments: the first is the callback function that we want to execute, and the second specifies the timeout...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found