You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have list of chunks which and I want to process each chunk one by one.
I want to execute chunk [0, 0, 0, 0] first, then [1, 1, 1, 1] then [2, 2, 2, 2].
But all the tasks in chunk should run parallely at same time.
Any way of doing this?
import 'dart:async';
import 'package:async_task/async_task.dart';
void main() async {
//List of chunks
List<List<int>> chunks = [
[0, 0, 0, 0], //chunk contains 4 tasks which should run parallely
[1, 1, 1, 1], //this chunk only gets executed once previous chunk is done
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7],
];
for (final chunk in chunks) {
threadExecutor(chunk);
}
}
void threadExecutor(List chunk) async {
List<TaskExecutor> tasks = [];
for (final task in chunk) {
tasks.add(TaskExecutor(task));
}
var asyncExecutor = AsyncExecutor(
sequential: false,
parallelism: 4,
taskTypeRegister: _taskTypeRegister,
);
asyncExecutor.logger.enabled = false;
var executions = asyncExecutor.executeAll(tasks);
await Future.wait(executions);
for (var task in tasks) {
var n = task.n;
var prime = task.result;
print('$n\t-> $prime \t $task');
}
}
List<AsyncTask> _taskTypeRegister() => [TaskExecutor(0)];
class TaskExecutor extends AsyncTask<int, dynamic> {
final int n;
TaskExecutor(this.n);
@override
AsyncTask<int, dynamic> instantiate(int parameters,
[Map<String, SharedData>? sharedData]) {
return TaskExecutor(parameters);
}
@override
int parameters() {
return n;
}
@override
FutureOr<dynamic> run() async {
return await myTask(n);
}
Future myTask(int n) async {
await Future.delayed(Duration(seconds: 1));
return n;
}
}
I have list of chunks which and I want to process each chunk
one by one
.I want to execute chunk
[0, 0, 0, 0]
first, then[1, 1, 1, 1]
then[2, 2, 2, 2]
.But all the tasks in chunk should run parallely at same time.
Any way of doing this?
Output:
The text was updated successfully, but these errors were encountered: