搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案