搜尋套件、儲存庫與 APP 分類

套件詳情v3.2.1

computer

易於使用的併發工作線程池,具有類似 compute() 的 API。

AndroidiOSmacOSWindowsLinux
近 30 天下載
1390
喜歡數
63
Pub 分數
160

安裝套件

flutter pub add computer

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

該電腦套件在 Dart 中提供一個簡單高效且可並行的工作人員池,提供類似於 Flutter 內建 compute 函數的 compute() API。它能將 CPU 負載密集型任務移至主要隔離區外執行,提升應用程式的回應速度。設計上強調易用性,抽象化複雜的併發管理,同時支援 Android、iOS、macOS、Windows 及 Linux 等多個平台。適用於背景處理,例如資料解析、影像處理或繁重運算。

適用情境

  • 在背景中執行CPU密集型作業
  • 處理大型數據集而不阻塞UI
  • 執行圖像或檔案轉換
  • 執行長時間運行的數學計算
  • 在密集型任務期間提升應用性能

主要特色

  • 簡單的 compute() API
  • 跨平台支援
  • 自動工作池管理
  • 輕量且易於整合
  • 內建的錯誤處理和重試

適合對象

  • 需要背景計算的開發人員
  • 資料處理密集型應用
  • 在高負載下需要響應式UI的專案
  • 希望獲得 Flutter compute 的即插即用替代方案的團隊
  • 針對多個桌面和移動平台的應用程式

使用建議

  • 不適合I/O密集型任務
  • 對工作人員行為的自訂有限
  • 在許多並發任務下可能消耗更多記憶體
  • 最適合用於CPU密集型而非網路作業
  • 需要仔細處理各隔離區之間的大數據傳輸

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Computer

Computer is a lightweight library for concurrent computations, which provides Flutter's compute like API.

Features

  • Easy to use API
  • No overhead on creating & releasing isolates for each task. Workers initialized on start and ready to solve your tasks
  • Strictly defined number of workers

Note

Computer.shared() is a singleton, use Computer.create() to create new instances

How to use

Computer provides just 3 methods

turnOn()

Before using the Computer you need to turnOn it. This will create workers and initialize them. Then you may use compute() method.

final computer = Computer.create(); //Or Computer.shared()

await computer.turnOn(
  workersCount: 4, // optional, default 2
  verbose: false, // optional, default false
);
compute()

compute will execute your function inside one of the workers. Function may be async. The callback argument must be a top-level or static method of a class.

final result = await computer.compute(
  fib,
  param: 45, // optional
);
turnOff()

If you don't need workers anymore, you can turnOff the Computer. It will destroy workers.

await computer.turnOff();
Errors

ComputerError the parent error for all errors/exceptions in this package

class ComputerError implements Exception {
  final String message;
}

RemoteExecutionError shows up when something goes wrong in task

class RemoteExecutionError extends ComputerError {
  final Capability taskCapability;
}

CancelExecutionError shows up when turnOff is called but task is processing

class CancelExecutionError extends ComputerError {
  final Capability taskCapability;
}

所屬分類

原始碼儲存庫

使用此套件的專案