Skip to content

Add compute method to easily run a method in an isolate #40238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nioncode opened this issue Jan 20, 2020 · 6 comments
Closed

Add compute method to easily run a method in an isolate #40238

nioncode opened this issue Jan 20, 2020 · 6 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-isolate type-enhancement A request for a change that isn't a bug

Comments

@nioncode
Copy link

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.

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-isolate type-enhancement A request for a change that isn't a bug labels Jan 20, 2020
@lrhn
Copy link
Member

lrhn commented Jan 20, 2020

The dart:isolate library is a fairly low-level library with very few convenience methods, which is why the isolate package exists with a more directly usable API.
I'd just use the isolate package, that's what it's there for. I'm sure it's possible to write code that is slightly smaller and faster than the more general IsolateRunner code ... but not much faster because most of the overhead comes from creating the isolate.

@MisterJimson
Copy link

The isolate package is now discontinued. Curious how that impacts your guidance above @lrhn.

@lrhn
Copy link
Member

lrhn commented Jan 3, 2022

I'm seriously considering adding a static run method to isolate, like

 static Future<T> run<T>(FutureOr<T> Function() computation);

which runs a function in a new (Isolate.spawn'ed) isolate.

With the improvements the VM has made on passing closures and other tricky values to other isolates, and with the Isolate.exit function to send the result back, it seems like the time is ripe for an efficient implementation.

@absar
Copy link

absar commented Oct 5, 2022

@lrhn was there any progress on this one, or any new recommendations?

@lrhn
Copy link
Member

lrhn commented Oct 5, 2022

The Isolate.run method is released in Dart 2.19.

@mkustermann
Copy link
Member

We have now a Isolate.run() method. So I think we can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-isolate type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants