搜索插件、代码仓与 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;
}

所属分类

源码仓库

使用此插件的项目