pool static method

Future<List<ProcessResultData>> pool(
  1. List<List<String>> commands, {
  2. String? workingDirectory,
  3. Map<String, String>? environment,
  4. Duration? timeout = const Duration(seconds: 60),
  5. int concurrency = 4,
})

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>();
}