Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Created August 10, 2020 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjbanov/f735e14eb3b21dc7fe410d8b4fba6ab4 to your computer and use it in GitHub Desktop.
Save yjbanov/f735e14eb3b21dc7fe410d8b4fba6ab4 to your computer and use it in GitHub Desktop.
Using package:pool to parallelize work across multiple processes
import 'dart:async';
import 'dart:io';
import 'package:pool/pool.dart';
Pool pool = Pool(8);
main() async {
final input = List<int>.generate(80, (i) => i);
final results = pool.forEach(
input,
_convertToString,
);
await for (String result in results) {
print(result);
}
}
/// Converts [i] to string "Result i" by calling `echo`.
/// Also calls `sleep` to simulate a long-running process (otherwise
/// `echo` is too quick).
Future<String> _convertToString(int i) async {
await Process.run('sleep', ['1']);
return (await Process.run('echo', ['Result $i'])).stdout;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment