5

I recently started with Dart (www.dartlang.org) and really like it so far. A very promising feature are isolates, but I am not sure on how to start.

The documentation I found so far is from before a breaking change (BREAKING CHANGE: dart:isolate) in October 2013. The information in this "Breaking change" email is quite complicated and it looks like the new api is more complicated than the old.

I've got some questions:

  • Is the dart:isolate api stable?
  • Is there any up-to-date documentation?
  • Are there any working examples?
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75

1 Answers1

3

I tried this example and it works https://gist.github.com/olostan/7883315

import "dart:isolate";

void main() {
  print("Starting");
  var sPort = new ReceivePort();
  SendPort rPort;
  sPort.listen((msg) {
    if (msg is SendPort) {
      print("Host got port. sending back");
      rPort = msg;
      rPort.send("Hello!");
    }
    else print("Host got $msg");
    rPort.send(null);
    sPort.close();
  });
  Isolate.spawn(test,sPort.sendPort);
}
void test(sender) {
  var rPort = new ReceivePort();
  sender.send(rPort.sendPort);
  rPort.listen((msg){
    print("Worker got $msg");
    if (msg!=null)
      sender.send("I am worker");
    else rPort.close();
  });
}

Isolates seem not to be used too much yet so there may still be some bugs.
The latest problems I remember reading about was debugging code running in isolates. I don't know if this is solved yet.
It also depends if you want to use isolates on the server or in the browser.
AFAIK it's more stable in the VM.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Hi Günter, Thanks. I've never used Dart on a server. On Dartium I get "spawnFunction is not supported from a dom-enabled isolate". Do you know if this is going to work in the future? (I know I can use spawnUri as shown here: http://japhr.blogspot.nl/2013/11/new-fangled-dart-isolates.html, but that looks like more cumbersome). – Hendrik Jan Jan 07 '14 at 13:42
  • Oh, I had planned to add this link too but obviously forgot. Glad you found it anyway. I saw in Chris' post that the `spawnFunction()` is not supported` is thrown even when actually `spawn()` was used. I don't have more information about this architecture either. I suggest posting a question on the Dart group that you referenced by the `BREAKING CHANGE` post. – Günter Zöchbauer Jan 07 '14 at 14:02
  • 2
    @hendrik - see for more examples [is-there-any-example-for-darts-spawnuri-in-library-dartisolate](https://stackoverflow.com/questions/17299512/is-there-any-example-for-darts-spawnuri-in-library-dartisolate/25104337). It seems that the additional overhead with `Isolate.spawnUri` is 1) putting your code in a separate library 2) dealing with the `Future` that tells you if the isolate spawned ok, from there on it it's all identical. – Argenti Apparatus Aug 03 '14 at 14:08
  • If I add `sleep(new Duration(seconds: 5));` after `Isolate.spawn` line, execution will be paused before spawning. Why? And how to create loops then? – OZ_ Sep 14 '14 at 14:28
  • `spawn` is async, this means that it is executed **after** the current 'thread of execution' has executed all sync code but the `sleep` halts the current 'thread' and therefore all further execution is prevented until sleep is finished. You can use `new Future.delayed(new Duration(seconds: 5), () { your delayed code here;});` to delay the execution of the code after `isolate.spawn()`. `sleep` should be avoided at all cost in production code. – Günter Zöchbauer Sep 14 '14 at 14:34