pool static method
Runs commands at the same time, at most concurrency of them, and
answers their results in the order given.
Implementation
static Future<List<ProcessResultData>> pool(
List<List<String>> commands, {
String? workingDirectory,
Map<String, String>? environment,
Duration? timeout = const Duration(seconds: 60),
int concurrency = 4,
}) async {
if (concurrency < 1) {
throw RangeError.range(concurrency, 1, null, 'concurrency');
}
final results = List<ProcessResultData?>.filled(commands.length, null);
var next = 0;
Future<void> worker() async {
while (next < commands.length) {
final index = next++;
results[index] = await run(
commands[index],
workingDirectory: workingDirectory,
environment: environment,
timeout: timeout,
);
}
}
final workers = concurrency < commands.length
? concurrency
: commands.length;
await Future.wait([for (var i = 0; i < workers; i++) worker()]);
return results.cast<ProcessResultData>();
}