Dreckr / Worker

Utility for easy concurrency with isolates
MIT License
35 stars 10 forks source link

Starting more tasks then processors exist results in a exception #5

Closed nkratzke closed 10 years ago

nkratzke commented 10 years ago

Starting more tasks then processors exists results in a exception. The following code

import "package:worker/worker.dart";
import "dart:async";

class FibTask extends Task {
  final _v;
  FibTask(this._v);
  execute() => fib(_v);
}

num fib(n) {
  if (n == 0) return 0;
  if (n == 1) return 1;
  return fib(n-1) + fib(n-2);
}

void main() {
  final isolates = new Worker();
  var parallels = [];
  for (var i in [10, 20, 30, 40, 30, 20]) {
    parallels.add(isolates.handle(new FibTask(i)));
  }

  Future.wait(parallels).then((r) {
    print(r);
  });
}

results on a four processor system into the following exception (in _WorkerIsolateImpl getter scheduledTask).

Breaking on exception: type 'EfficientLengthMappedIterable<_ScheduledTask, dynamic>' is not a subtype of type 'List<Task>' of 'function result'.

Everything works fine if less tasks then processors exist are started. So

  final isolates = new Worker();
  var parallels = [];
  for (var i in [10, 20, 30]) {
    parallels.add(isolates.handle(new FibTask(i)));
  }

  Future.wait(parallels).then((r) {
    print(r);
  });

works fine on a four processor system.

Dreckr commented 10 years ago

Fixed on 08e5ad78892747854e5f6cbeb9f0ca0049c8df76

Remember to close your Worker instance on Future.wait handler:

Future.wait(parallels).then((r) {
    print(r);
    isolates.close();
  });

Thanks for reporting! You are awesome!

Edit: I'll release a version ASAP.

Dreckr commented 10 years ago

@nkratzke, may I add your code to the other examples?

nkratzke commented 10 years ago

Of course. Nane Am 15.02.2014 23:46 schrieb "Diego Rocha" notifications@github.com:

@nkratzke https://github.com/nkratzke, may I add your code to the other examples?

Reply to this email directly or view it on GitHubhttps://github.com/Dreckr/Worker/issues/5#issuecomment-35170467 .