GithubHelp home page GithubHelp logo

Comments (3)

svenwanzenried avatar svenwanzenried commented on July 23, 2024

I would be interested in the also.
Currently I am thinking that I would run the whole state machine in a separate thread, and that I have to queue the incoming event myself?

from python-statemachine.

fgmacedo avatar fgmacedo commented on July 23, 2024

The best approach depends on the demand. Since we don't have an internal loop that "run forever", one can implement that method that reads from a stream of events and send the events to the state machine, and this method can live in a dedicated thread.

Do you have a problem idea so I can work on an example?

from python-statemachine.

fgmacedo avatar fgmacedo commented on July 23, 2024

We've improved the support for threading with a mutex that does multi-threading synchronization on 2.3.2 (not released yet), but basic threading (run on another thread) is possible even on older versions.

Please take a look at tests/test_threading.py for a few examples.

def test_regression_443_with_modifications():
"""
Test for https://github.com/fgmacedo/python-statemachine/issues/443
"""
time_collecting = 0.2
time_to_send = 0.125
time_sampling_current_state = 0.05
class TrafficLightMachine(StateMachine):
"A traffic light machine"
green = State(initial=True)
yellow = State()
red = State()
cycle = green.to(yellow) | yellow.to(red) | red.to(green)
def __init__(self, name):
self.name = name
self.statuses_history = []
super().__init__()
def beat(self):
waiting_time = 0
sent = False
while waiting_time < time_collecting:
if waiting_time >= time_to_send and not sent:
self.cycle()
sent = True
self.statuses_history.append(f"{self.name}.{self.current_state.id}")
time.sleep(time_sampling_current_state)
waiting_time += time_sampling_current_state
class Controller:
def __init__(self, name):
self.fsm = TrafficLightMachine(name)
# set up thread
t = threading.Thread(target=self.fsm.beat)
t.start()
c1 = Controller("c1")
c2 = Controller("c2")
c3 = Controller("c3")
time.sleep(time_collecting + 0.01)
assert c1.fsm.statuses_history == ["c1.green", "c1.green", "c1.green", "c1.yellow"]
assert c2.fsm.statuses_history == ["c2.green", "c2.green", "c2.green", "c2.yellow"]
assert c3.fsm.statuses_history == ["c3.green", "c3.green", "c3.green", "c3.yellow"]

from python-statemachine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.