|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +================= |
| 4 | +stopit.processstop |
| 5 | +================= |
| 6 | +
|
| 7 | +Control the timeout of blocks or callables with a context manager or a |
| 8 | +decorator. Based on the use of multiprocessing for enforcing timeouts. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import multiprocessing |
| 14 | +from typing import Callable, Any |
| 15 | + |
| 16 | +from .utils import TimeoutException, BaseTimeout, base_timeoutable |
| 17 | + |
| 18 | + |
| 19 | +def process_target(block: Callable, args: tuple, kwargs: dict, return_dict: dict) -> None: |
| 20 | + """Run the block of code in a subprocess. |
| 21 | +
|
| 22 | + :param block: The function to execute in the subprocess. |
| 23 | + :param args: Positional arguments for the block function. |
| 24 | + :param kwargs: Keyword arguments for the block function. |
| 25 | + :param return_dict: Shared dictionary to store the result or error. |
| 26 | + """ |
| 27 | + try: |
| 28 | + # Call the block function with provided arguments and store the result |
| 29 | + result = block(*args, **kwargs) |
| 30 | + return_dict['result'] = result |
| 31 | + except Exception as e: |
| 32 | + # Store the error in return_dict |
| 33 | + return_dict['error'] = str(e) |
| 34 | + |
| 35 | + |
| 36 | +class ProcessTimeout(BaseTimeout): |
| 37 | + """Context manager for enforcing timeouts using multiprocessing. |
| 38 | +
|
| 39 | + See :class:`stopit.utils.BaseTimeout` for more information |
| 40 | + """ |
| 41 | + def __init__(self, seconds: int, swallow_exc: bool = True) -> None: |
| 42 | + super().__init__(seconds, swallow_exc) |
| 43 | + self.process: multiprocessing.Process | None = None |
| 44 | + self.manager: multiprocessing.Manager | None = None |
| 45 | + self.return_dict: multiprocessing.Dict | None = None |
| 46 | + self.block: Callable | None = None |
| 47 | + self.args: tuple = () |
| 48 | + self.kwargs: dict = {} |
| 49 | + |
| 50 | + def set_block(self, block: Callable, *args: Any, **kwargs: Any) -> None: |
| 51 | + """Set the block of code to execute |
| 52 | + """ |
| 53 | + if not callable(block): |
| 54 | + raise ValueError("Block function must be callable.") |
| 55 | + self.block = block |
| 56 | + self.args = args |
| 57 | + self.kwargs = kwargs |
| 58 | + |
| 59 | + def setup_interrupt(self) -> None: |
| 60 | + """Setting up the resource that interrupts the block |
| 61 | + """ |
| 62 | + if not self.block: |
| 63 | + raise ValueError("No block function provided for execution.") |
| 64 | + |
| 65 | + self.manager = multiprocessing.Manager() |
| 66 | + self.return_dict = self.manager.dict() |
| 67 | + |
| 68 | + # Start the subprocess |
| 69 | + self.process = multiprocessing.Process( |
| 70 | + target=process_target, args=(self.block, self.args, self.kwargs, self.return_dict) |
| 71 | + ) |
| 72 | + self.process.start() |
| 73 | + |
| 74 | + # Wait for the process to complete or timeout |
| 75 | + self.process.join(self.seconds) |
| 76 | + if self.process.is_alive(): |
| 77 | + # If still alive after timeout, terminate and raise TimeoutException |
| 78 | + self.process.terminate() |
| 79 | + self.state = self.TIMED_OUT |
| 80 | + raise TimeoutException(f"Block exceeded maximum timeout value ({self.seconds} seconds).") |
| 81 | + |
| 82 | + def suppress_interrupt(self) -> None: |
| 83 | + """Removing the resource that interrupts the block |
| 84 | + """ |
| 85 | + if self.process and self.process.is_alive(): |
| 86 | + self.process.terminate() # Ensure the process is terminated |
| 87 | + if 'error' in self.return_dict: |
| 88 | + raise Exception(f"Error during execution: {self.return_dict['error']}") |
| 89 | + if self.manager: |
| 90 | + self.manager.shutdown() |
| 91 | + |
| 92 | + def get_result(self) -> Any: |
| 93 | + """Retrieve the result of the block execution |
| 94 | + """ |
| 95 | + if self.return_dict and 'result' in self.return_dict: |
| 96 | + return self.return_dict['result'] |
| 97 | + return None |
| 98 | + |
| 99 | + |
| 100 | +class process_timeoutable(base_timeoutable): # noqa: B903 |
| 101 | + """A function or method decorator that raises a ``TimeoutException`` for |
| 102 | + decorated functions that exceed a certain amount of time. This uses the |
| 103 | + ``ProcessTimeout`` context manager. |
| 104 | +
|
| 105 | + See :class:`.utils.base_timeoutable`` for further comments. |
| 106 | + """ |
| 107 | + def __init__(self) -> None: |
| 108 | + super().__init__() |
| 109 | + self.to_ctx_mgr = ProcessTimeout |
0 commit comments