worker2 1.0.1

  • Installing
  • Versions
  • 68

Worker #

An easy to use utility to perform tasks concurrently.

By performing blocking CPU intensive tasks concurrently, you free your main isolate to do other stuff while you take advantage of the CPU capabilities.

Usage #

To use this library, create a new Worker that will handle the isolates for you, encapsulate your task in class that implements the Task interface and pass it to the Worker to execute it:

void main () {
	Worker worker = new Worker();
	Task task = new AckermannTask(1, 2);
	worker.handle(task).then((result) => print(result));

class AckermannTask implements Task {
  int x, y;

  AckermannTask (this.x, this.y);

  int execute () {
    return ackermann(x, y);

  int ackermann (int m, int n) {
    if (m == 0)
      return n+1;
    else if (m > 0 && n == 0)
      return ackermann(m-1, 1);
      return ackermann(m-1, ackermann(m, n-1));

You can also define how many tasks a worker can execute concurrently and if the isolates should be spawned lazily:

Worker worker = new Worker(poolSize: 4, spawnLazily: false);

If you want to manage the isolates and SendPorts yourself but still use Tasks, WorkerIsolate comes to the rescue:

WorkerIsolate isolate = new WorkerIsolate();
isolate.performTask(new AckermannTask(1, 2))

You can perform multple http get requests in parallel for example :

import 'dart:async';
import 'package:worker2/worker2.dart';
import 'dart:io';
import 'dart:convert';

void main() {
  final worker = new Worker(poolSize: 4, spawnLazily: false);
  var futures = <Future>[];
  for (var url in ["","",""]) {

  Future.wait(futures).then((r) {


Future<String> getUrl(String url) async {
  Completer completer = new Completer();
  StringBuffer contents = new StringBuffer();
  var request = await HttpClient().getUrl(Uri.parse(url));
  var response = await request.close(); 
 response.transform(utf8.decoder).listen((String data) {
  }, onDone: () => completer.complete(contents.toString()));
  return await completer.future;

class AsyncHttpGet implements Task<Future<String>> {
  String url;
  bool throwException;

  AsyncHttpGet (this.url, {this.throwException: false});

  Future<String> execute () {
    return getUrl(url);

Tips #

  • A Task may return a Future. The Worker will wait until this Future is completed and will return its result.
  • If you have to perform many iterations of an operation, you can batch it into tasks and run them concurrently.
  • Running tasks in other isolates involves copying the task object to the other isolate. Keep your task thin.
  • Always use benchmarks to identify if Worker is really helping and if the amount of isolates is ideal.

Changelog #

1.0.1 #

Made changes to upgrade the pub score

1.0.0 #

Upgraded package Worker to be Dart2 compatible (#14)

0.5.0 #

Workers and WorkerIsolates now can wait until all tasks have been completed before closing. ([#10] (

0.4.0 #

Streams of events have been added, giving better monitoring capabilities. (#11)

0.3.11 #

Loosen stack_trace min version dependency. (#12)

0.3.10 #

Support tasks that do not return. (#8)

0.3.9 #

Proper error handling with stackTrace support. (#7)

0.3.8 #

WorkerIsolate factory. (#6)

0.3.7 #

Bug fix.

0.3.6 #

Minor bug fix.

0.3.5 #

Bug fix.

0.3.4 #

Improved isolate selection and load distribution.

0.3.3 #

Major bug fix.

0.3.2 #

Major bug fix.

0.3.1 #

Bug fixes.

0.3.0 #

Work with new Isolates API.

0.2.2 #

Minor bug fix.

0.2.1 #

Bug fixes.

0.2.0 #

Major rewrite:

  • Isolates now may handle more than one Task at the time.
  • Creation of the WorkerIsolate to abstract comunication with isolates.
  • 'execute' method from Worker renamed to 'handle'.
  • Workers and WorkerIsolates may be closed.
  • Errors are now handled properly.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  worker2: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:worker2/worker2.dart';
Version Uploaded Documentation Archive
1.0.1 Feb 22, 2019 Go to the documentation of worker2 1.0.1 Download worker2 1.0.1 archive
1.0.0 Feb 22, 2019 Go to the documentation of worker2 1.0.0 Download worker2 1.0.0 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jun 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18


Detected platforms: Flutter, other

Primary library: package:worker2/worker2.dart with components: io, isolate.

Health suggestions

Fix lib/src/worker_impl.dart. (-5.36 points)

Analysis of lib/src/worker_impl.dart reported 11 hints, including:

line 50 col 7: DO use curly braces for all flow control structures.

line 61 col 7: DO use curly braces for all flow control structures.

line 66 col 7: DO use curly braces for all flow control structures.

line 68 col 7: DO use curly braces for all flow control structures.

line 106 col 11: DO use curly braces for all flow control structures.

Fix lib/src/worker.dart. (-1.49 points)

Analysis of lib/src/worker.dart reported 3 hints:

line 13 col 1: Prefer using /// for doc comments.

line 68 col 1: Prefer using /// for doc comments.

line 113 col 1: Prefer using /// for doc comments.

Fix lib/src/events.dart. (-0.50 points)

Analysis of lib/src/events.dart reported 1 hint:

line 45 col 24: Don't explicitly initialize variables to null.

Fix lib/src/worker_isolate_src.dart. (-0.50 points)

Analysis of lib/src/worker_isolate_src.dart reported 1 hint:

line 32 col 9: DO use curly braces for all flow control structures.

Maintenance issues and suggestions

Documentation URL isn't helpful. (-10 points)

Either remove the documentation field from pubspec.yaml, or update it to link to a website about the package.

The package description is too short. (-18 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and worker2.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.67.0 <3.0.0
stack_trace >=0.9.0 <2.0.0 1.9.3
Transitive dependencies
path 1.6.2
Dev dependencies
test 1.3.0