Skip to content

Instantly share code, notes, and snippets.

@plateaukao
Last active May 28, 2019 07:18
Show Gist options
  • Save plateaukao/b4e5fd32f2a39d5161584d4a6818b53c to your computer and use it in GitHub Desktop.
Save plateaukao/b4e5fd32f2a39d5161584d4a6818b53c to your computer and use it in GitHub Desktop.
isolate_example
Isolate isolate;
void startRealAsyncTask() async {
// need a ReceivePort to receive messages.
ReceivePort receivePort= ReceivePort();
isolate = await Isolate.spawn(heavyTask, receivePort.sendPort);
receivePort.listen((data) {
stdout.write('RECEIVE: ' + data + ', ');
});
}
void heavyTask(SendPort sendPort) {
// doing something very heavy here.
String msg = "I'm done";
sendPort.send(msg);
}
void stop() {
if (isolate != null) {
stdout.writeln('killing isolate');
isolate.kill(priority: Isolate.immediate);
isolate = null;
}
}
void main() async {
stdout.writeln('spawning isolate...');
await startRealAsyncTask();
stdout.writeln('press enter key to quit...');
await stdin.first;
stop();
stdout.writeln('goodbye!');
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment