worker_manager 2.3.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 92

Executor #

GitHub Logo

Executor is a library for running CPU intensive functions inside a separate dart isolate. This is useful if you want to avoid skipping frames when the main isolate is rendering the UI. Since isolates are able to run when the main thread is created, make sure that your functions that are added to the Executor task queue are static or defined globally (just in a dart file, not inside a class).

Notice #

  • Executor - is a Singleton, meaning there is only ever one instance of Executor.

Usage #

1st step: Initialize Executor (this is not necessary, but recommended. Executor initialization flow based on available processors number ). Write this code inside main function (make sure that you main is async):

 await Executor().warmUp();

2nd step: Create a Task with the runnable you wish to run in the isolate.

final task = Task(runnable: Runnable(arg1: Counter(), arg2: 40, fun2: calculate));

class Counter {
  int fib(int n) {
    if (n < 2) {
      return n;
    }
    return fib(n - 2) + fib(n - 1);
  }
}
int calculate(Counter counter, int arg) => counter.fib(arg);

3rd step: Call Executor.addTask(your task). Executor returns a Stream.

Executor().addTask(
    task: Task(
        runnable:: yourRunnable:,
        timeout: Duration(seconds: 25),
      ),
    ).listen((data) {
        handle with you result
      }).onError((error) {
        handle error
      });

Bonus: you can stop a task any time you want. Removing a task will produce nothing and will result in no data passed into the listen method.

final task = Task(function: fibonacci, bundle: 88);
Executor.addTask(task: task).listen((data){
        nothing here
    });
task.cancel();

Changelog #

2.3.0 #

  • added runnable

2.1.4 #

  • optimized removing task

2.0.9 #

  • added task types

2.0.7 #

  • updated readme and dependencies

2.0.5 #

  • fifo adding

2.0.4 #

  • clean up

2.0.1 #

  • removed ghost connection

2.0.0 #

  • removed ports hell

1.2.8 #

  • fixed bugs with removing same task twice

1.2.0 #

  • resolved deleting task bug

1.0.1 #

  • fixed timeout

0.9.8 #

  • added cash

0.9.1 #

  • added 1 thread constructor for fake fifo and threadPoolSize setter

0.7.8 #

  • code clean up

0.4.1 #

  • changed readme

0.2.3 #

  • added examples and readme

0.2.1 #

  • added canceling functionality

0.1.0 #

  • correct timeouts (canceling coming soon)

example/README.md

#example

// Copyright Daniil Surnin. All rights reserved.
// Use of this source code is governed by a Apache license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:worker_manager/worker_manager.dart';

void main() async {
  /* this is not necessary, this code will spawn
   before your awesome widgets will build,
   to avoid micro freezes
   if you don't want to spawn free of calculation isolates,
   just don't write this code :

await WorkerManager().initManager(); runApp(MyApp()); }

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( showPerformanceOverlay: true, home: MyHomePage(), ); } }

class MyHomePage extends StatefulWidget { MyHomePage({Key key}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); }

class _MyHomePageState extends State

/* creating task for workerManager with global function and Bundle class for your function. bundle and timeout is optional parameters. */ final task = Task(function: fib, bundle: 40, timeout: Duration(days: 78));

/*remember, that you global function must have only one parameter, like int, String or your bundle class . For example: Class Bundle { final int age; final String name; Bundle(this.age, this.name); } optional parameters is ok, just be ready to avoid NPE */ @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Container( height: 350, width: 200, color: Colors.cyan, child: Column( children:

@override void dispose() { // Good case when you want to end your hard calculations in dispose method workerManager.killTask(task: task); super.dispose(); } }

int fib(int n) { if (n < 2) { return n; } return fib(n - 2) + fib(n - 1); }

Future


Use this package as a library

1. Depend on it

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


dependencies:
  worker_manager: ^2.3.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:worker_manager/worker_manager.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
85
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

We analyzed this package on Jan 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health issues and suggestions

Document public APIs. (-1 points)

78 out of 78 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/executor.dart.

Run flutter format to format lib/executor.dart.

Format lib/isolate.dart.

Run flutter format to format lib/isolate.dart.

Format lib/runnable.dart.

Run flutter format to format lib/runnable.dart.

Format lib/scheduler.dart.

Run flutter format to format lib/scheduler.dart.

Format lib/task.dart.

Run flutter format to format lib/task.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
async ^2.4.0 2.4.0
flutter 0.0.0
uuid ^2.0.4 2.0.4
Transitive dependencies
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.4
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test