dynamic_parallel_queue

Creator: coderz1093

Last updated:

0 purchases

dynamic_parallel_queue Image
dynamic_parallel_queue Images

Languages

Categories

Add to Cart

Description:

dynamic parallel queue

Dynamic Parallel Queue #
Features #
Easy to use(so, no example file), Efficient, Pure
Getting started #
import 'package:dynamic_parallel_queue/dynamic_parallel_queue.dart';
copied to clipboard
Usage #
Serial queue #
void main() async {
// Now it's a serial queue, parallel default is 5
final queue = Queue(parallel: 1);

final List<int> res = [];

// Add a async function, return Future
final task1 = queue.add(() async {
await Future.delayed(Duration(milliseconds: 30));
res.add(1);
});
// You can wait it done.
await task1;

// add a list, return Future
final tasks = queue.addAll([
() async {
await Future.delayed(Duration(milliseconds: 20));
res.add(2);
},
() async {
await Future.delayed(Duration(milliseconds: 10));
res.add(3);
},
]);

/// You can wait for the tasks to complete
/// But not need here
// await tasks;


/// Remove all pending tasks
// queue.clear();

/// Wait for the queue to complete
await queue.whenComplete();
}
copied to clipboard
Although their wait times are different, buy they are executed in order.
Console output: [1, 2, 3]
Parallel Queue #
void main() async {
final queue = Queue();
final List<int> res = [];

queue.addAll([
() async {
await Future.delayed(Duration(milliseconds: 30));
res.add(1);
},
() async {
await Future.delayed(Duration(milliseconds: 20));
res.add(2);
},
() async {
await Future.delayed(Duration(milliseconds: 10));
res.add(3);
},
]);

/// Wait for the queue to complete
await queue.whenComplete();
print(res); // [3, 2, 1]
}
copied to clipboard
Serial queue change to parallel queue #
void main() async {
final queue = Queue(parallel: 1);
final List<int> res = [];
final task1 = queue.add(() async {
await Future.delayed(Duration(milliseconds: 50));

/// You can change it at any time.
queue.parallel = 5;

res.add(1);
});
await task1;
queue.addAll([
() async {
await Future.delayed(Duration(milliseconds: 30));
res.add(2);
},
() async {
await Future.delayed(Duration(milliseconds: 20));
res.add(3);
},
() async {
await Future.delayed(Duration(milliseconds: 10));
res.add(4);
},
]);
await queue.whenComplete();
print(res); // [1, 4, 3, 2]
}
copied to clipboard
About priority(default priority is 1) #
The fewer value is mean the higher priority.
void main() async {
final queue = Queue(parallel: 2);
final List<int> res = [];
queue.addAll([
() async => res.add(1),
() async => res.add(2),
], priority: 10); // Because the queue is empty, so it's running as soon as it's added
queue.add(() async => res.add(3), priority: 3);
queue.add(() async => res.add(4), priority: 2);
queue.add(() async => res.add(0), priority: -1); // Support negative number
await queue.whenComplete();
print(res); // [1, 2, 0, 4, 3]
}
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.