1

I'm spawning an isolate (first isolate) from main thread to make network calls to the backend to get a large amount of data and process them into local DB.

During this process when I spawn another isolate (second isolate) from same main thread to carry out another task. When the second isolate sends data to main thread through the SendPort once the task is completed it stops the tasks carried out by the first isolate. Any help please?

Is it something to do with receive port since both the isolates are spawned from main thread and both will have a port to the main thread to share memory, does closing any of the port causes other ports also to be closed?

3
  • "does closing any of the port causes other ports also to be closed?" - no Commented Aug 31, 2021 at 5:15
  • @pskink any reasons on why the other isolate stops? Commented Aug 31, 2021 at 7:03
  • @pskink unable to view the page Commented Aug 31, 2021 at 8:59

1 Answer 1

1

You can work with multiple isolates pointing to the same Main ReceivePort, but each isolate will need to have its own ReceivePort. For example:

Isolate 1 ReceivePort -> Main ReceivePort <- Isolate 2 Receive Port

But you should be careful about how you will receive and save the isolates ports on the Main Thread. Since they need to be send using the Main ReceivePort.

I recommend you to check out the plugin easy_isolate, it provides a simple way to work with multiple isolates with well-explained documentation.

https://pub.dev/packages/easy_isolate

Sign up to request clarification or add additional context in comments.

7 Comments

Im having separate receive port for each isolates
_isolate = await Isolate.spawn(checkOutBoxAndSendToServer, map); _receivePort.listen((message) { log("MESSAGE FROM ISOLATE : ${message.toString()}"); if(message.toString()=="stopOutbox"){ _receivePort.close(); } },onDone: (){ log("DONE"); this.stop(); });
void stop(){ _isolate!.kill(); _isolate = null;}
i'm sending a message as stopOutbox from isolate once the task is completed and on the main thread on receiving it i'm closing the receive port
_isolate = await Isolate.spawn(checkAndDownloadMessages, map);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.