-
-
Save emanuel-braz/e890c5930e4c307d9c5dfead2198af9d to your computer and use it in GitHub Desktop.
Dart: Isolate Example [Courtesy: Forian Loitsch]. NOTE: This example is not maintained. Please see https://github.com/dartlang-italia/dart-libraries-samples/tree/master/isolate for a working example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void echo(SendPort initialReplyTo) { | |
var port = new ReceivePort(); | |
initialReplyTo(port.sendPort); | |
port.listen((msg) { | |
var data = msg[0]; | |
SendPort replyTo = msg[1]; | |
replyTo.send(data); | |
if (data == "bar") port.close(); | |
}); | |
} | |
Future sendReceive(SendPort port, msg) { | |
ReceivePort response = new ReceivePort(); | |
port.send([msg, response.sendPort]); | |
return response.first; | |
} | |
main() { | |
var response = new ReceivePort(); | |
Future<Isolate> remote = Isolate.spawn(echo, response.sendPort); | |
remote.then((_) => response.first).then((sendPort) { | |
sendReceive(sendPort, "foo").then((msg) { | |
print("received: $msg"); | |
return sendReceive(sendPort, "bar"); | |
}).then((msg) { | |
print("received another: $msg"); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// == new file echo.dart. | |
void main(List<String> args, SendPort replyTo) { | |
replyTo.send(args[0]); | |
} | |
// == new file start.dart | |
main() { | |
var response = new ReceivePort(); | |
Future<Isolate> remote = Isolate.spawnUri(Uri.parse("echo.dart"), ["foo"], response.sendPort); | |
remote.then((_) => response.first) | |
.then((msg) { print("received: $msg"); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment