Skip to content

Instantly share code, notes, and snippets.

@olostan
Last active December 30, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olostan/7883315 to your computer and use it in GitHub Desktop.
Save olostan/7883315 to your computer and use it in GitHub Desktop.
Dart: two-way isolate communication
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();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment