Multiple Isolates Strategy

I’ve been working on a project to help autofill crossword grids which is computationally heavy so I’ve been using an Isolate to do that work, using the framework described in the isolate docs.

This works fine but oftentimes you want to stop this calculation and start a new one when new user input comes in, the user enters a new word into the grid for example, without having to wait for the first one to finish. I wasn’t able to come up with a way to stop the current calculation and start a new one mid way, so my idea is to have 1 long standing isolate that manages the messages from the Main Isolate and spawns a new isolate to do the calculation, then if a new message arrives it could kill that existing isolate and spawn a new one.

I was curious if the community had better ideas on how to handle a scenario like this where you don’t need an isolate to finish computing and instead restart, and also any tips or tricks on how anyone handles multiple isolates communicating before I dive in to trying to implement my idea.

1 Like

integral_isolates | Dart package with ReplaceBackpressureStrategy.

For your case, I’m not sure why you want to have a separate Isolate that manages messages? You can just have the main Isolate spawn new Isolates (cheap operation these days with Isolate groups) and then have it create a new Isolate and send the previously spawned Isolate a stop “command” if a new message comes in.

Main thing to keep in mind is if you have a very tight loop in the worker Isolate, to have somewhere in the loop where it will check for the stop messages as Isolates need to return to the run loop in order to process your incoming “stop command” via a ReceivePort.

4 Likes