-
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.
- Loading branch information
1 parent
e88ddea
commit a06a06c
Showing
3 changed files
with
71 additions
and
3 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,56 @@ | ||
from __future__ import annotations | ||
|
||
import subprocess | ||
import threading | ||
from contextlib import suppress | ||
|
||
__all__ = [] | ||
|
||
|
||
class Terminator: | ||
_process: subprocess.Popen | None | ||
|
||
def __init__(self, timeout: float) -> None: | ||
self._process = None | ||
self._timeout = timeout | ||
self._is_active = False | ||
|
||
@property | ||
def process(self) -> subprocess.Popen: | ||
if self._process is None: | ||
raise AttributeError("there is no process") | ||
return self._process | ||
|
||
@process.setter | ||
def process(self, process: subprocess.Popen) -> None: | ||
if self._process is not None: | ||
raise AttributeError("already has process") | ||
self._process = process | ||
self._start() | ||
|
||
@property | ||
def is_active(self) -> bool: | ||
return self._is_active | ||
|
||
@is_active.setter | ||
def is_active(self, value: bool) -> None: | ||
self._is_active = value | ||
|
||
def _start(self) -> None: | ||
self.thread = threading.Thread( | ||
target=terminate, args=(self._timeout, self.process, self) | ||
) | ||
self.thread.daemon = True | ||
self.thread.start() | ||
|
||
|
||
def terminate( | ||
timeout: float, process: subprocess.Popen, terminator: Terminator | ||
) -> None: | ||
try: | ||
with suppress(TimeoutError, subprocess.TimeoutExpired): | ||
process.wait(timeout) | ||
finally: | ||
if process.returncode is None: | ||
process.terminate() | ||
terminator.is_active = True |