パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v7.2.9

worker_manager

worker_manager を使用すると、Isolate を介してCPU集約的な計算を管理できます

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
5.3万
いいね数
399
Pub ポイント
145

パッケージをインストール

flutter pub add worker_manager

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

worker_manager パッケージは、CPU を多く使用するタスクを Dart の isolate に分散することで、効率的な管理を可能にし、アプリの応答性とパフォーマンスを向上させます。メイン UI スレッドをブロックせずに、バックグラウンド計算の作成、実行、調整を簡素化します。大量のデータ処理や画像操作、複雑な計算に適しており、isolate の通信やライフサイクルイベントを扱うための明確な API を提供します。このパッケージはクロスプラットフォームで、Android、iOS、Web、macOS、Windows、Linux をサポートしています。

用途

  • 大規模なデータ処理
  • 画像またはファイルの変換
  • UI アプリケーションにおけるバックグラウンド計算
  • リアルタイム分析
  • 複雑な数学演算

主な機能

  • Isolate を用いた並行処理
  • タスク管理のためのシンプルな API
  • クロスプラットフォーム対応
  • 自動エラー処理
  • タスクライフサイクルの監視

適している対象

  • パフォーマンスが重要なアプリケーション
  • 濃密なバックグラウンドタスクを持つアプリ
  • 隔離管理が必要な開発者
  • 応答性のあるUIを必要とするFlutterアプリ
  • 複数のプラットフォームをターゲットにするプロジェクト

注意事項

  • 孤立体の理解が必要です
  • CPUバウンドタスクに限定
  • 潜在的なメモリオーバーヘッド
  • I/O集約的な操作には適していません
  • 隔離間での状態管理に注意が必要です

元の 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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト