Description
This is basically a re-post of flutter/flutter#48673, since I was advised to file this on this project instead.
It would be great if dart would offer a standard library method for simply running a method in an isolate, i.e. something along the lines of:
Future<R> run_background<R, P> (FutureOr<R> function(P argument), P argument) async {
IsolateRunner iso = await IsolateRunner.spawn();
try {
return await iso.run(function, argument);
} finally {
await iso.close();
}
}
Please note that this example uses IsolateRunner.run from the isolate package.
Flutter provides the compute() method for this.
However, in order to create a library that can also be run in a pure dart setting (i.e. without flutter), we need a method that behaves like compute
in the dart sdk.
Is the method I showed above good enough or are there any disadvantages when using IsolateRunner.run
instead of compute
? For a standard library implementation, we should probably get rid of IsolateRunner
though or merge it into the sdk.
One nice thing about IsolateRunner
is that we can keep a long lived instance of it and dispatch methods to the isolate when needed and not incur the cost of spawning up an isolate.