搜索插件、代码仓与 APP 分类

插件详情v7.2.9

worker_manager

worker_manager 允许您在隔离区之间管理计算密集型操作

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
5.3万
喜欢数
399
Pub 分数
145

安装插件

flutter pub add worker_manager

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

worker_manager 包通过将 CPU 密集型任务分发到 Dart 隔离区,实现了高效的任务管理,提升了应用程序的响应性和性能。它简化了后台计算的创建、执行和协调,而不会阻塞主 UI 线程。适用于大量数据处理、图像操作或复杂计算,提供简洁的 API 来处理隔离区通信和生命周期事件。该包支持跨平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 大量数据处理
  • 图像或文件转换
  • UI 应用中的后台计算
  • 实时分析
  • 复杂的数学运算

主要特点

  • 基于 Isolate 的并发
  • 任务管理的简单 API
  • 跨平台支持
  • 自动错误处理
  • 任务生命周期监控

适合对象

  • 性能关键的应用程序
  • 具有密集后台任务的应用程序
  • 需要隔离管理的开发者
  • 需要响应式 UI 的 Flutter 应用
  • 面向多个平台的项目

使用建议

  • 需要理解隔离区
  • 仅限于CPU密集型任务
  • 潜在的内存开销
  • 不适合用于输入/输出密集型操作
  • 需要在隔离区之间进行仔细的状态管理

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Worker Manager

Worker Manager is a powerful and easy-to-use library that helps you efficiently manage CPU-intensive tasks in your Flutter applications. It offers several advantages over traditional async programming or the built-in compute method. Note that the functions you are passing should either be static methods, globally defined functions or lambdas to ensure they are accessible from other isolates.

New Feature - dynamicSpawn flag in init

By using this flag, worker_manager changes its behavior regarding isolate management. If this flag is set to true, new isolates will be spawned only if necessary and killed when they are no longer needed.

Advantages

Efficient Scheduling

This library schedules CPU-intensive functions to avoid skipping frames or freezes in your Flutter application. It ensures that your app runs smoothly, even when dealing with heavy computations.

Reusable Isolates

Unlike the compute method, which always creates a new Dart isolate, Worker Manager reuses existing isolates. This approach is more efficient and prevents overloading the CPU. When resources are not freed up, using the compute method may cause freezes and skipped frames.

Cancelable Tasks

Worker Manager provides a cancellation functionality through the Cancelable class and its cancel method. This feature allows developers to free up resources when they are no longer needed, improving the app's performance and responsiveness.

Gentle Cancellation

In addition to the standard cancellation method, Worker Manager offers a more gentle approach to cancellation. This new feature, executeGentle, does not immediately terminate the Dart isolate but instead provides a lambda function that can be called periodically within the task to check if it should be cancelled. This allows tasks to clean up and terminate gracefully.

Inter-Isolate Communication

The library supports communication between isolates with the executeWithPort method. This feature enables sending progress messages or other updates between isolates, providing more control over your tasks.

Gentle Cancellation with Port

The executeGentleWithPort method is another valuable feature of Worker Manager that combines gentle cancellation and inter-isolate communication. It accepts two arguments: a port for inter-isolate communication and a getter function isCancelled for gentle cancellation. This offers more flexibility and control, allowing tasks to communicate with other isolates while also providing an opportunity for graceful termination.

Usage

Execute the task

Cancelable<ResultType> cancelable = workerManager.execute<ResultType>(
  () async {
    // Your CPU-intensive function here
  },
  priority: WorkPriority.immediately,
);

Execute a Task with Gentle Cancellation

Cancelable<ResultType> cancelable = workerManager.executeGentle<ResultType>(
  (isCanceled) async {
    while (!isCanceled()) {
      // Your CPU-intensive function here
      // Check isCanceled() periodically to decide whether to continue or break the loop
    }
  },
  priority: WorkPriority.immediately,
);

Execute a Task with Inter-Isolate Communication

Cancelable<ResultType> cancelable = workerManager.executeWithPort<ResultType, MessageType>(
  (SendPort sendPort) async {
    // Your CPU-intensive function here
    // Use sendPort.send(message) to communicate with the main isolate
  },
  onMessage: (MessageType message) {
    // Handle the received message in the main isolate
  },
);

Execute a Task with Gentle Cancellation and Inter-Isolate Communication

Cancelable<ResultType> cancelable = workerManager.executeGentleWithPort<ResultType, MessageType>(
  (SendPort sendPort, IsCanceled isCanceled) async {
    while (!isCanceled()) {
      // Your CPU-intensive function here
      // Use sendPort.send(message) to communicate with the main isolate
      // Check isCanceled() periodically to decide whether to continue or break the loop
    }
  },
  onMessage: (MessageType message) {
    // Handle the received message in the main isolate
  },
  priority: WorkPriority.immediately,
);

Cancel a Task

cancelable.cancel();

Dispose Worker Manager

await workerManager.dispose();

Notice

Before isolates support in wasm, implementation of execution of task same as compute method from foundation

Conclusion

By using Worker Manager, you can enjoy the benefits of efficient task scheduling, reusable isolates, cancellable tasks, and inter-isolate communication. It provides a clear advantage over traditional async programming and the built-in compute method, ensuring that your Flutter applications remain performant and responsive even when handling CPU-intensive tasks. The additional executeGentleWithPort feature provides both gentle cancellation and inter-isolate communication for your tasks, offering further control and efficiency.

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目