tasks 0.1.0

  • README.md
  • Installing
  • Versions
  • 30

Tasks

Allows to specify "tasks" and execute them concurrently in a single thread in a queue with predefined concurrency count.

Features

You can:

  • Define tasks (by inheriting from Task or using BlockTask), which will perform some work.
  • Define task queues, where you can specify the number of concurrently executing tasks in the queue.
  • Track the status of the tasks (pending, executing, finished, etc).
  • Cancel the tasks.
  • Specify priorities for the tasks, which will be taken into account when deciding what task will execute next in the queue.

What's it good for:

  • Concurrent handling of IO heavy or network heavy tasks. Like, you have 1000 HTTP calls to be made, and you want to parallelize them by making 20 at a time. This library will be a good fit for that task.
  • Concurrent handling of tasks where you want to have the track of the status. E.g. image uploading, where you want to track the progress, and filter by successfully uploaded ones.
  • You got the idea :)

What's it not good for:

  • CPU heavy concurrent tasks. Since it uses single-threaded Future-based tasks, it will block the thread. You probably need something isolates-based instead.

Usage

Using BlockTask to handle simultaneous HTTP calls:

import 'dart:async';
import 'dart:convert';
import 'package:tasks/tasks.dart';
import 'package:http/http.dart' as http;

main() async {
  var taskQueue = new TaskQueue(concurrencyCount: 3);
  var pages = new List.generate(10, (i) => i);
  var allPackages = [];
  var results = await Future.wait(pages.map((page) async {
    var task = new BlockTask(() async {
      return JSON.decode((await http.get("https://pub.dartlang.org/packages.json?page=$page")).body)["packages"];
    });
    taskQueue.queue(task);
    var result = await task.result;
    allPackages.addAll(result);
  }));
  print(allPackages);
}

Using pmap

This scenario above is pretty common, so we added a convenience helper pmap for that:

import 'dart:async';
import 'dart:convert';
import 'package:tasks/utils.dart';
import 'package:http/http.dart' as http;

main() async {
  var pages = new List.generate(10, (i) => i);
  var allPackages = await pmap(pages, (page) async {
    return JSON.decode((await http.get("https://pub.dartlang.org/packages.json?page=$page")).body)["packages"];
  }, concurrencyCount: 3);
  print(allPackages.expand((i) => i).toList());
}

Using subclasses of Task

If you have way more complicated requirements, with the business logic not fitting nicely into one anonymous function, you can create your own class for Task and just override some methods:

import 'dart:async';
import 'dart:convert';
import 'package:tasks/tasks.dart';
import 'package:http/http.dart' as http;

class MyTask extends Task<Iterable<String>> {
  final int page;
  MyTask(this.page, {int priority: TaskPriority.normal}) : super(priority: priority);

  Future<Iterable<String>> performWork() async {
    return JSON.decode((await http.get("https://pub.dartlang.org/packages.json?page=$page")).body)["packages"];
  }
}

main() async {
  var taskQueue = new TaskQueue(concurrencyCount: 3);
  var pages = new List.generate(10, (i) => i);
  var allPackages = [];
  var results = await Future.wait(pages.map((page) async {
    var task = new MyTask(page);
    taskQueue.queue(task);
    var result = await task.result;
    allPackages.addAll(result);
  }));
  print(allPackages);
}

Tracking status of tasks

Let's extend the previous example to be able to track when the task is queued and finished.

// ...
var task = new MyTask(page);
task.onStateChange.listen((_) {
  if (task.isExecuting) {
    print("Task for page ${task.page} is executing");
  } else if (task.isFinished) {
    print("Task for page ${task.page} is finished");
  }
});
// ...

1. Depend on it

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


dependencies:
  tasks: "^0.1.0"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:tasks/tasks.dart';
        
Version Uploaded Documentation Archive
0.1.0 Nov 7, 2015 Go to the documentation of tasks 0.1.0 Download tasks 0.1.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Jan 21, 2018
  • Dart: 2.0.0-dev.15.0
  • pana: 0.10.0

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
0 / 100
Health:
Code health derived from static analysis. [more]
98 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]
30

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:tasks/tasks.dart.

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Use constrained dependencies.

    The pubspec.yaml contains 2 dependencies without version constraints. Specify version ranges for the following dependencies: collection, frappe.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use tasks.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <2.0.0
collection any 1.14.5
frappe any 0.4.0+6
Transitive dependencies
stream_transformers 0.3.0+3
Dev dependencies
http any
test any