commit | 79b88df6f0a0a6d96f216ed3253a98a325941a2e | [log] [tgz] |
---|---|---|
author | Greg Spencer <gspencergoog@users.noreply.github.com> | Mon Oct 26 10:24:22 2020 -0700 |
committer | GitHub <noreply@github.com> | Mon Oct 26 10:24:22 2020 -0700 |
tree | a5c7284215a833bde36284067570b87b04a08d57 | |
parent | c1a04398fe8e3f2b45a6607ed24f6f251cae6d31 [diff] | |
parent | 05a869b94d3533ee565b4c260a66df1f29ca9d00 [diff] |
Merge pull request #19 from jakemac53/allow-2.12 Allow 2.12 prerelease Dart sdks
The process_runner
package for Dart uses the ProcessManager
class from process
package to allow invocation of external OS processes, and manages the stderr and stdout properly so that you don't lose any output, and can easily access it without needing to wait on streams.
Like dart:io
and process
, it supplies a rich, Dart-idiomatic API for spawning OS processes, with the added benefit of easy retrieval of stdout and stderr from the result of running the process, with proper waiting for the process and stderr/stdout streams to be closed. Because it uses process
, you can supply a mocked ProcessManager
to allow testing of code that uses process_runner
.
In addition to being able to launch processes separately with ProcessRunner
, it allows creation of a pool of worker processes with ProcessPool
, and manages running them with a set number of active WorkerJob
s, and manages the collection of their stdout, stderr, and interleaved stdout and stderr output.
See the example and process_runner
library docs for more information on how to use it, but the basic usage for is:
import 'package:process_runner/process_runner.dart'; Future<void> main() async { ProcessRunner processRunner = ProcessRunner(); ProcessRunnerResult result = await processRunner.runProcess(['ls']); print('stdout: ${result.stdout}'); print('stderr: ${result.stderr}'); // Print interleaved stdout/stderr: print('combined: ${result.output}'); }
For the ProcessPool
, also see the example, but it basically looks like this:
import 'package:process_runner/process_runner.dart'; Future<void> main() async { ProcessPool pool = ProcessPool(numWorkers: 2); final List<WorkerJob> jobs = <WorkerJob>[ WorkerJob(['ls'], name: 'Job 1'), WorkerJob(['df'], name: 'Job 2'), ]; await for (final WorkerJob job in pool.startWorkers(jobs)) { print('\nFinished job ${job.name}'); } }
Or, if you just want the answer when it's done:
import 'package:process_runner/process_runner.dart'; Future<void> main() async { ProcessPool pool = ProcessPool(numWorkers: 2); final List<WorkerJob> jobs = <WorkerJob>[ WorkerJob(['ls'], name: 'Job 1'), WorkerJob(['df'], name: 'Job 2'), ]; List<WorkerJob> finishedJobs = await pool.runToCompletion(jobs); for (final WorkerJob job in finishedJobs) { print("${job.name}: ${job.result.stdout}"); } }