worker_manager 1.1.0 copy "worker_manager: ^1.1.0" to clipboard
worker_manager: ^1.1.0 copied to clipboard

outdated

Executor allows you to create a queue of tasks for threadpool of isolates

Executor is instrument to run any functions inside separate dart isolate. This is useful if you want to avoid lags and freezes in UI isolate(main). If you parse big json data or calculating math it can make your app laggy. To solve this problem, make sure that you functions, which working with Executor is static or defined as global (just in a dart file, not inside a class).

First step: Executor - is a Singleton, you can call Executor everywhere you want, it will not produce any instance except first one.

Second step: You can set threadPool size for Executor like this:

Executor(threadPoolSize: 2);

If you want to warm up isolates, write this code inside main function (make sure that you main is async)

 await Executor(threadPoolSize: 2).warmUp();

Third step: To run code inside isolate you should create a Task

final task = Task<parameter type, return type>(function: yourFunction,
 bundle: one parameter for your function, it can be empty
 timeout: Duration( time for calculation) - optional parameter
);

Fourth step: Call Executor.addTask(you task). it will return Stream with result. Here is example:

Executor().addTask<parameter type, return type>(
    task: Task(function: yourFunction, bundle: parameter,
     timeout: Duration(seconds: 25))).listen((data) {
                handle with you result
              }).onError((error) {
                handle error
              });

Bonus: you can stop task every time you want. Removing task will produce nothing and you will not get any data inside listen method.

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

Optional: you can end work with Executor().

Executor().stop();
290
likes
0
pub points
94%
popularity

Publisher

verified publisherrenesanse.net

Executor allows you to create a queue of tasks for threadpool of isolates

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

async, flutter

More

Packages that depend on worker_manager