python-statemachine icon indicating copy to clipboard operation
python-statemachine copied to clipboard

Enable Multithreading

Open carminoplata opened this issue 2 years ago • 2 comments

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?

carminoplata avatar Sep 11 '23 12:09 carminoplata

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?

svenwanzenried avatar May 07 '24 14:05 svenwanzenried

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 avatar May 07 '24 15:05 fgmacedo

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

fgmacedo avatar Jun 26 '24 19:06 fgmacedo