Monday, February 27, 2012

Upcoming changes to the isolate API

Posted by Sigmund Cherem

We are actively working on improving the isolate APIs. If you have code that uses isolates today, some of our upcoming changes will likely break your code. Here are a few of the changes coming up:
  • We are moving the isolates code out of corelib into a standalone 'dart:isolate' library.
  • We are making any top-level static function a valid entrypoint for an isolate, so you'll no longer have to create a class to define an isolate's entrypoint.
  • We want to simplify other parts of the API too, like how to obtain a port after spawning an isolate.
  • Later on, we plan to support spawning an isolate given a URL to its code.
For example, this code written with today's API:

// soon to be deprecated!
class MyIsolate extends Isolate {
  MyIsolate() : super.heavy();

  main() {
    port.receive((msg, reply) => reply.send("re: $msg"));
  }
}

main() {
  MyIsolate myIsolate = new MyIsolate();
  myIsolate.spawn().then((SendPort sendPort) {
    sendPort.call("hi").receive((msg, _) => print(msg));
  });
}


could be written as follows:

// coming soon!
#import('dart:isolate', prefix: 'isolate');

isolateCode() {
  isolate.port.receive((msg, reply) => reply.send("re: $msg"));
}

main() {
  SendPort sendPort = isolate.spawnFunction(isolateCode);
  sendPort.call("hi").then(print);
}


There are many details still under debate, so watch news.dartlang.org for updates when these and future changes land in trunk.