-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
pueblo.util.proc.process
utility
It is a wrapper around `subprocess.Popen` to also terminate child processes after exiting.
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import subprocess | ||
import typing as t | ||
from contextlib import contextmanager | ||
|
||
import psutil | ||
|
||
|
||
@contextmanager | ||
def process(*args, **kwargs) -> t.Generator[subprocess.Popen, None, None]: | ||
""" | ||
Wrapper around `subprocess.Popen` to also terminate child processes after exiting. | ||
Implementation by Pedro Cattori. Thanks! | ||
-- https://gist.github.com/jizhilong/6687481#gistcomment-3057122 | ||
""" | ||
proc = subprocess.Popen(*args, **kwargs) # noqa: S603 | ||
try: | ||
yield proc | ||
finally: | ||
try: | ||
children = psutil.Process(proc.pid).children(recursive=True) | ||
except psutil.NoSuchProcess: | ||
return | ||
for child in children: | ||
child.kill() | ||
proc.kill() |
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,32 @@ | ||
import pytest | ||
|
||
from pueblo.util.proc import process | ||
|
||
|
||
def test_process_success(tmp_path): | ||
outfile = tmp_path / "myfile.out.log" | ||
with process(["echo", "Hello, world."], stdout=open(outfile, "w")) as proc: | ||
assert isinstance(proc.pid, int) | ||
|
||
with open(outfile, "r") as fp: | ||
assert fp.read() == "Hello, world.\n" | ||
|
||
|
||
def test_process_noop(): | ||
process(["mycommand", "myarg", "--myoption", "myoptionvalue"]) | ||
|
||
|
||
def test_process_failure_command_not_found(): | ||
with pytest.raises(FileNotFoundError) as ex: | ||
with process(["mycommand", "myarg", "--myoption", "myoptionvalue"]): | ||
pass | ||
assert ex.match("No such file or directory") | ||
|
||
|
||
def test_process_failure_in_contextmanager(): | ||
with pytest.raises(ZeroDivisionError): | ||
with process(["echo", "Hello, world."]) as proc: | ||
print(proc.pid) # noqa: T201 | ||
# Even though this throws an exception, the `process` contextmanager | ||
# will *still* clean up the process correctly. | ||
0 / 0 # noqa: B018 |