-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: util for pooled subprocesses
- Loading branch information
Showing
3 changed files
with
71 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import subprocess | ||
|
||
|
||
def complete(process: subprocess.Popen) -> subprocess.CompletedProcess: | ||
"""Allow a subprocess to complete and return a `CompletedProcess`. | ||
`CompletedProcess` is otherwise returned only by the high-level | ||
function `subprocess.run`. Here, `run`-style functionality is | ||
recreated for a given `Popen` object: | ||
* the process is allowed to complete (and the invoking thread is stalled) | ||
* standard output and error are collected (via `communicate()`) | ||
* a `CompletedProcess` is returned reflecting the process's command | ||
argumentation, returncode, standard output and error | ||
This is of primary use to subprocesses launched via `Popen` for the | ||
purpose of parallelization. (Otherwise, higher-level interfaces may | ||
be used.) | ||
""" | ||
(stdout, stderr) = process.communicate() | ||
|
||
return subprocess.CompletedProcess( | ||
process.args, | ||
process.returncode, | ||
stdout, | ||
stderr, | ||
) |