async_task는 Android, iOS, Web, macOS, Windows, Linux를 포함한 모든 플랫폼에서 비동기 작업을 실행하고 병렬 실행을 관리하는 것을 간편하게 해주는 Dart 패키지입니다. 개발자가 Isolates의 복잡성을 다룰 필요 없이 스레드 풀과 유사한 추상화를 제공합니다. 이 패키지는 플랫폼 간 전환에 있어 투명한 동시성 처리를 보장하여 효율적으로 작업을 오프로드하면서도 깔끔하고 읽기 쉬운 코드를 유지할 수 있게 해줍니다. 백그라운드 처리, 데이터 가져오기 또는 CPU 집약적인 작업에 적합합니다.
사용 사례
백그라운드 처리
병렬 작업 실행
CPU 집약적 작업
백그라운드에서 데이터 가져오기
크로스 플랫폼 동시성
작업 스케줄링
주요 기능
플랫폼 간의 동시성
격리 관리 필요 없음
내장형 병렬 실행기
다양한 플랫폼 간 투명성
비동기 작업을 위한 간단한 API
모든 Dart 플랫폼을 지원합니다
적합한 대상
간편한 동시성을 원하는 개발자
백그라운드 작업이 필요한 앱
크로스 플랫폼 프로젝트
Isolate 복잡성을 피하는 팀
고성능 애플리케이션
실시간 데이터 처리
고려 사항
장시간 블로킹 작업에 적합하지 않습니다
기본 스레드에 대한 제어가 제한됨
메모리 관리를 신중하게 해야 할 수 있습니다
단기에서 중기까지의 작업에 최적
Dart의 이벤트 루프 동작에 따라 다릅니다
중첩된 비동기 작업 체인 피하기
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
async_task
This package brings asynchronous tasks and parallel executors (similar to classic thread pools) for all Dart platforms
(JS/Web, Flutter, VM/Native) through transparent internal implementations, based on dart:isolate or
only dart:async, without having to deal with the Isolate complexity.
Motivation
Dart parallelism is based on asynchronous, non-blocking and thread-safe code. This creates a language that facilitates
the creation of safe concurrency code, but all running in the same thread, using only 1 thread/core of a device.
If you want to use more than 1 thread/core of a device, Dart VM/Native has Isolate, but the paradigm is not easy to
use like classical Thread Pools, or like environments that shares all the memory/objects between threads.
This package was created to facilitate the creation and execution of multiple tasks asynchronously in all Dart
Platforms, avoiding platform specific codes by the developer.
Usage
import'dart:async';
import'package:async_task/async_task.dart';
voidmain() async {
// The tasks to execute:var tasks = [
PrimeChecker(8779),
PrimeChecker(1046527),
PrimeChecker(3139581), // Not PrimePrimeChecker(16769023),
];
// Instantiate the task executor:var asyncExecutor = AsyncExecutor(
sequential: false, // Non-sequential tasks.
parallelism: 2, // Concurrency with 2 threads.
taskTypeRegister: _taskTypeRegister, // The top-level function to register the tasks types.
);
// Enable logging output:
asyncExecutor.logger.enabled = true ;
// Execute all tasks:var executions = asyncExecutor.executeAll(tasks);
// Wait tasks executions:awaitFuture.wait(executions);
for (var task in tasks) {
var n = task.n; // Number to check for prime.var prime = task.result; // Task result: true if is prime.print('$n\t-> $prime \t $task');
}
}
// This top-level function returns the tasks types that will be registered// for execution. Task instances are returned, but won't be executed and// will be used only to identify the task type:List<AsyncTask> _taskTypeRegister() => [PrimeChecker(0)];
// A task that checks if a number is prime:classPrimeCheckerextendsAsyncTask<int, bool> {
// The number to check if is prime.finalint n;
PrimeChecker(this.n);
// Instantiates a `PrimeChecker` task with `parameters`.@overrideAsyncTask<int, bool> instantiate(int parameters) {
returnPrimeChecker(parameters);
}
// The parameters of this task:@overrideintparameters() {
return n;
}
// Runs the task code:@overrideFutureOr<bool> run() {
returnisPrime(n);
}
// A simple prime check function:boolisPrime(int n) {
if (n < 2) returnfalse;
var limit = n ~/ 2;
for (var p = 2; p <= limit; ++p) {
if (n % p == 0) returnfalse;
}
returntrue;
}
}
Note that the parameter parallelism at AsyncExecutor shouldn't be greater than the CPU cores
(unless you really have a case that justifies a higher number).
The most common error is to put a high number for the parameter parallelism thinking that this will increase tasks
execution, but this actually can reduce your application performance. Note that each allocated Isolateneeds a CPU
core to execute, and a high number of Isolate instances running tasks will be a bottleneck in your system
(specially if you are demanding more Isolate than CPU cores).
SharedData
The class SharedData facilitates and optimizes data shared between tasks.
The main advantage of data encapsulated with SharedData is to avoid multiple
messages, with the same data, between threads/isolates, avoiding concurrency
performance issues and multiple duplicated objects in memory (a GC bottleneck).
Here's an example of a task using SharedData:
// A task that checks if a number is prime:classPrimeCheckerextendsAsyncTask<int, bool> {
// The number to check if is prime.finalint n;
// A list of known primes, shared between tasks.finalSharedData<List<int>, List<int>> knownPrimes;
PrimeChecker(this.n, this.knownPrimes);
// Instantiates a `PrimeChecker` task with `parameters` and `sharedData`.@overridePrimeCheckerinstantiate(int parameters, [Map<String, SharedData>? sharedData]) {
returnPrimeChecker(
parameters,
sharedData!['knownPrimes'] asSharedData<List<int>, List<int>>,
);
}
// The `SharedData` of this task.@overrideMap<String, SharedData> sharedData() => {'knownPrimes': knownPrimes};
// Loads the `SharedData` from `serial` for each key.@overrideSharedData<List<int>, List<int>> loadSharedData(String key, dynamic serial) {
switch (key) {
case'knownPrimes':
returnSharedData<List<int>, List<int>>(serial);
default:
throwStateError('Unknown key: $key');
}
}
// The parameters of this task:@overrideintparameters() {
return n;
}
// Runs the task code:@overrideFutureOr<bool> run() {
returnisPrime(n);
}
// A simple prime check function:boolisPrime(int n) {
if (n < 2) returnfalse;
// The pre-computed primes, optimizing this checking algorithm:if (knownPrimes.data.contains(n)) {
returntrue;
}
// It's sufficient to search for prime factors in the range [1,sqrt(N)]:var limit = (sqrt(n) + 1).toInt();
for (var p = 2; p < limit; ++p) {
if (n % p == 0) returnfalse;
}
returntrue;
}
}
The field knownPrimes above will be shared between tasks. In platforms with support
for dart:isolateknownPrimes will be sent through an Isolate port only once,
avoiding multiple copies and unnecessary memory allocations.
AsyncTaskChannel
An AsyncTask can have a communication channel that can be used to send/received messages
during task execution.
To use a task channel just override channelInstantiator, than use channelResolved() inside
the task and await channel() outside it:
classYourTaskextendsAsyncTask<String, int> {
// ...@overrideAsyncTaskChannel? channelInstantiator() => AsyncTaskChannel(); // You can extend `AsyncTaskChannel` class if needed.// ...@overrideFutureOr<int> run() async {
// ...// get the resolved channel:var channel = channelResolved()!; // The channel is always resolved inside `run()`.// Send and wait a message in the channel:
channel.send('some message');
var result = await channel.waitMessage<String>();
// Send and wait in a single method:var result = await channel.sendAndWaitResponse<String, String>('some message');
// Read a message if available in the queue (non-blocking):var msgOptional = channel.readMessage<String>();
// ...
}
}
Outside communication with the task:
voidmain() async {
// ...// Execute the task:
asyncExecutor.execute(task);
// ...// Wait for the channel to be resolved:var channel = (await task.channel())!;
// Wait for a message:var msg = await channel.waitMessage();
// process msg...// Send a response:
channel.send('Some response');
// ...
}
An AsyncTaskChannel is automatically closed when a task finishes (returns its result).