-
From the documentation I understand that it is recommended to use KStateMachine in the single thread. I have an application in which I need to create multiple StateMachines. Imagine I have a following GraphQL endpoint
But I'm not sure if I this is correct approach. I need to have multiple State Machines running concurrently and not to block each other. When I tried to run multiple concurrent request I was getting |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, no You can find correct scope configuration samples in a docs https://kstatemachine.github.io/kstatemachine/#multithreading-and-concurrency |
Beta Was this translation helpful? Give feedback.
-
Thanks for explanation but I guess |
Beta Was this translation helpful? Give feedback.
No, newSingleThreadContext function just creates a thread and a CoroutineScope uses it to run coroutines inside. So it blocks calling thread only for a short time (means it is non-blocking)
Each call to newSingleThreadContext starts new thread. This is resource allocation so don't forget to close it when you are done with it's machine.
State machines that are attached to different threads such way, will run concurrently (to each other). As I understood that is what you need.
Keep in mind that thread allocation is not so cheap as coroutine startup. And if your method is called frequently you can get an error from OS that too many threads were created (about 200 I suppose). So thread pool s…