dart-archive / isolate

Makes working with Dart isolates easier.
https://pub.dev/packages/isolate
BSD 3-Clause "New" or "Revised" License
90 stars 34 forks source link

Is isolate.registry capable for multi cores environment? #29

Closed rockingdice closed 5 years ago

rockingdice commented 5 years ago

I see the comments in the package:

/// When the registry is shared across isolates, both elements and tags must
/// be sendable between the isolates.
/// Between isolates spawned using [Isolate.spawn] from the same initial
/// isolate, most objects can be sent.
/// Only simple objects can be sent between isolates originating from different
/// [Isolate.spawnUri] calls.

Does the initial isolate have to be the root isolate? If using custom objects instead of base values, will it limit the use for multi cores environment? If I want to take the advantage of multi-cores, any tips on better to do / not to do when using the registry?

lrhn commented 5 years ago

Isolates are either spawned using spawn or spawnUri. The spawnUri function creates a completely new isolate running a different Dart script/program. There is no guarantee that a class existing in the spawner isolate also exists in the spawnee isolate, so sending of objects between such isolates is restricted to simple list/map structures.

When you use spawn, you create a copy of the current isolate. It has all the same classes available, and many more objects can be sent between two such two isolates.

You can see spawn as creating a new isolate in the same "family" of isolates as the spawning isolate. Each call to spawn in a family will extend the family. Any call to spawnUri will create a new family, which can the create more isolates using spawn.

So, the initiial isolate does not have to be the root isolate, it's just the first isolate of an isolate spawn-family. It's not special inside that family either, and it can terminate while other isolates in the same family keep running.

The Dart isolate communication is not concurrency based, each isolate runs single-threaded, and communication is only depending on the way the isolate was spawned. Running on multiple cores has no relation to this.

So, decide up-front how you are going to structure your isolate families. If the communicating isolates belong to different families, limit the data to simple types and list/maps of those.

rockingdice commented 5 years ago

@lrhn I think I'm quite clear now, thank you!