Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is mostly to make things a bit more separate since there’s a circular dependency, which is not great. Signed-off-by: Stefan Marr <git@stefan-marr.de>
- Loading branch information
Showing
3 changed files
with
73 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from os import kill | ||
from signal import SIGKILL | ||
from subprocess import PIPE, Popen | ||
|
||
IS_PY3 = None | ||
|
||
try: | ||
_ = ProcessLookupError | ||
IS_PY3 = True | ||
except NameError: | ||
IS_PY3 = False | ||
|
||
# Indicate timeout with standard exit code | ||
E_TIMEOUT = -9 | ||
|
||
|
||
def _kill_py2(proc_id, uses_sudo): | ||
if uses_sudo: | ||
from .denoise import deliver_kill_signal | ||
|
||
deliver_kill_signal(proc_id) | ||
return | ||
|
||
try: | ||
kill(proc_id, SIGKILL) | ||
except IOError: | ||
# it's a race condition, so let's simply ignore it | ||
pass | ||
|
||
|
||
def _kill_py3(proc_id, uses_sudo): | ||
if uses_sudo: | ||
from .denoise import deliver_kill_signal | ||
|
||
deliver_kill_signal(proc_id) | ||
return | ||
|
||
try: | ||
kill(proc_id, SIGKILL) | ||
except ProcessLookupError: # pylint: disable=undefined-variable | ||
# it's a race condition, so let's simply ignore it | ||
pass | ||
|
||
|
||
def kill_process(pid, recursively, thread, uses_sudo): | ||
pids = [pid] | ||
if recursively: | ||
pids.extend(_get_process_children(pid)) | ||
|
||
for proc_id in pids: | ||
if IS_PY3: | ||
_kill_py3(proc_id, uses_sudo) | ||
else: | ||
_kill_py2(proc_id, uses_sudo) | ||
|
||
if thread: | ||
thread.join() | ||
return E_TIMEOUT, thread.stdout_result, thread.stderr_result | ||
return E_TIMEOUT, None, None | ||
|
||
|
||
def _get_process_children(pid): | ||
# pylint: disable-next=consider-using-with | ||
proc = Popen('pgrep -P %d' % pid, shell=True, stdout=PIPE, stderr=PIPE) | ||
stdout, _stderr = proc.communicate() | ||
result = [int(p) for p in stdout.split()] | ||
for child in result[:]: | ||
result.extend(_get_process_children(child)) | ||
return result |
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