fgmacedo / python-statemachine

Python Finite State Machines made easy.
MIT License
858 stars 84 forks source link

Enable Multithreading #403

Closed carminoplata closed 2 months ago

carminoplata commented 1 year ago

Hi there,

I looked at the code because I saw that when the first event is sent, the thread which is the owner of the state machine is blocked until the whole chain of events is fully consumed. Is there any possibility to update it in order to make the statemachine fully managed by a separate thread?

svenwanzenried commented 4 months ago

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?

fgmacedo commented 4 months ago

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?

fgmacedo commented 2 months ago

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.

https://github.com/fgmacedo/python-statemachine/blob/d446d99ba7c3f35884a43a576d18f4daea6e6850/tests/test_threading.py#L78-L127