{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "# Timeout\n", "\n", "The code in this notebook helps in interrupting execution after a given time." ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "**Prerequisites**\n", "\n", "* This notebook needs some understanding on advanced concepts in Python, notably \n", " * classes\n", " * the Python `with` statement\n", " * the Python `signal` functions\n", " * measuring time" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "## Synopsis\n", "\n", "\n", "To [use the code provided in this chapter](Importing.ipynb), write\n", "\n", "```python\n", ">>> from debuggingbook.Timeout import \n", "```\n", "\n", "and then make use of the following features.\n", "\n", "\n", "The `Timeout` class throws a `TimeoutError` exception after a given timeout has expired.\n", "Its typical usage is in conjunction with a `with` clause:\n", "\n", "```python\n", ">>> try:\n", ">>> with Timeout(0.2):\n", ">>> some_long_running_function()\n", ">>> print(\"complete!\")\n", ">>> except TimeoutError:\n", ">>> print(\"Timeout!\")\n", "Timeout!\n", "\n", "```\n", "Note: On Unix/Linux systems, the `Timeout` class uses [`SIGALRM` signals](https://docs.python.org/3.10/library/signal.html) (interrupts) to implement timeouts; this has no effect on performance of the tracked code. On other systems (notably Windows), `Timeout` uses the [`sys.settrace()`](https://docs.python.org/3.10/library/sys.html?highlight=settrace#sys.settrace) function to check the timer after each line of code, which affects performance of the tracked code.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Measuring Time\n", "\n", "The class `Timeout` allows interrupting some code execution after a given time interval." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:44.778211Z", "iopub.status.busy": "2023-11-12T13:02:44.778085Z", "iopub.status.idle": "2023-11-12T13:02:44.811626Z", "shell.execute_reply": "2023-11-12T13:02:44.811301Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import bookutils.setup" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:44.813546Z", "iopub.status.busy": "2023-11-12T13:02:44.813387Z", "iopub.status.idle": "2023-11-12T13:02:44.815094Z", "shell.execute_reply": "2023-11-12T13:02:44.814789Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:44.816753Z", "iopub.status.busy": "2023-11-12T13:02:44.816574Z", "iopub.status.idle": "2023-11-12T13:02:44.818501Z", "shell.execute_reply": "2023-11-12T13:02:44.818166Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# ignore\n", "from typing import Type, Any, Callable, Union, Optional" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:44.820238Z", "iopub.status.busy": "2023-11-12T13:02:44.820098Z", "iopub.status.idle": "2023-11-12T13:02:44.821960Z", "shell.execute_reply": "2023-11-12T13:02:44.821539Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from types import FrameType, TracebackType" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Variant 1: Unix (using signals, efficient)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:44.824292Z", "iopub.status.busy": "2023-11-12T13:02:44.824083Z", "iopub.status.idle": "2023-11-12T13:02:44.826444Z", "shell.execute_reply": "2023-11-12T13:02:44.825947Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import signal" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:44.828583Z", "iopub.status.busy": "2023-11-12T13:02:44.828446Z", "iopub.status.idle": "2023-11-12T13:02:44.832347Z", "shell.execute_reply": "2023-11-12T13:02:44.832001Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "class SignalTimeout:\n", " \"\"\"Execute a code block raising a timeout.\"\"\"\n", "\n", " def __init__(self, timeout: Union[int, float]) -> None:\n", " \"\"\"\n", " Constructor. Interrupt execution after `timeout` seconds.\n", " \"\"\"\n", " self.timeout = timeout\n", " self.old_handler: Any = signal.SIG_DFL\n", " self.old_timeout = 0.0\n", "\n", " def __enter__(self) -> Any:\n", " \"\"\"Begin of `with` block\"\"\"\n", " # Register timeout() as handler for signal 'SIGALRM'\"\n", " self.old_handler = signal.signal(signal.SIGALRM, self.timeout_handler)\n", " self.old_timeout, _ = signal.setitimer(signal.ITIMER_REAL, self.timeout)\n", " return self\n", "\n", " def __exit__(self, exc_type: Type, exc_value: BaseException,\n", " tb: TracebackType) -> None:\n", " \"\"\"End of `with` block\"\"\"\n", " self.cancel()\n", " return # re-raise exception, if any\n", "\n", " def cancel(self) -> None:\n", " \"\"\"Cancel timeout\"\"\"\n", " signal.signal(signal.SIGALRM, self.old_handler)\n", " signal.setitimer(signal.ITIMER_REAL, self.old_timeout)\n", "\n", " def timeout_handler(self, signum: int, frame: Optional[FrameType]) -> None:\n", " \"\"\"Handle timeout (SIGALRM) signal\"\"\"\n", " raise TimeoutError()" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "Here's an example:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:44.834402Z", "iopub.status.busy": "2023-11-12T13:02:44.834258Z", "iopub.status.idle": "2023-11-12T13:02:44.836458Z", "shell.execute_reply": "2023-11-12T13:02:44.836078Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def some_long_running_function() -> None:\n", " i = 10000000\n", " while i > 0:\n", " i -= 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:44.838348Z", "iopub.status.busy": "2023-11-12T13:02:44.838201Z", "iopub.status.idle": "2023-11-12T13:02:45.045199Z", "shell.execute_reply": "2023-11-12T13:02:45.044859Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Timeout!\n" ] } ], "source": [ "try:\n", " with SignalTimeout(0.2):\n", " some_long_running_function()\n", " print(\"Complete!\")\n", "except TimeoutError:\n", " print(\"Timeout!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Variant 2: Generic / Windows (using trace, not very efficient)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:45.066716Z", "iopub.status.busy": "2023-11-12T13:02:45.066548Z", "iopub.status.idle": "2023-11-12T13:02:45.068397Z", "shell.execute_reply": "2023-11-12T13:02:45.068091Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:45.070103Z", "iopub.status.busy": "2023-11-12T13:02:45.069997Z", "iopub.status.idle": "2023-11-12T13:02:45.073519Z", "shell.execute_reply": "2023-11-12T13:02:45.073143Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "class GenericTimeout:\n", " \"\"\"Execute a code block raising a timeout.\"\"\"\n", "\n", " def __init__(self, timeout: Union[int, float]) -> None:\n", " \"\"\"\n", " Constructor. Interrupt execution after `timeout` seconds.\n", " \"\"\"\n", "\n", " self.seconds_before_timeout = timeout\n", " self.original_trace_function: Optional[Callable] = None\n", " self.end_time: Optional[float] = None\n", "\n", " def check_time(self, frame: FrameType, event: str, arg: Any) -> Callable:\n", " \"\"\"Tracing function\"\"\"\n", " if self.original_trace_function is not None:\n", " self.original_trace_function(frame, event, arg)\n", "\n", " current_time = time.time()\n", " if self.end_time and current_time >= self.end_time:\n", " raise TimeoutError\n", "\n", " return self.check_time\n", "\n", " def __enter__(self) -> Any:\n", " \"\"\"Begin of `with` block\"\"\"\n", " start_time = time.time()\n", " self.end_time = start_time + self.seconds_before_timeout\n", "\n", " self.original_trace_function = sys.gettrace()\n", " sys.settrace(self.check_time)\n", " return self\n", "\n", " def __exit__(self, exc_type: type, \n", " exc_value: BaseException, tb: TracebackType) -> Optional[bool]:\n", " \"\"\"End of `with` block\"\"\"\n", " self.cancel()\n", " return None # re-raise exception, if any\n", "\n", " def cancel(self) -> None:\n", " \"\"\"Cancel timeout\"\"\"\n", " sys.settrace(self.original_trace_function)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Again, our example:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:45.075185Z", "iopub.status.busy": "2023-11-12T13:02:45.075077Z", "iopub.status.idle": "2023-11-12T13:02:45.277245Z", "shell.execute_reply": "2023-11-12T13:02:45.276931Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Timeout!\n" ] } ], "source": [ "try:\n", " with GenericTimeout(0.2):\n", " some_long_running_function()\n", " print(\"Complete!\")\n", "except TimeoutError:\n", " print(\"Timeout!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Choosing the right variant" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:45.279026Z", "iopub.status.busy": "2023-11-12T13:02:45.278914Z", "iopub.status.idle": "2023-11-12T13:02:45.280824Z", "shell.execute_reply": "2023-11-12T13:02:45.280537Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "Timeout: Type[SignalTimeout] = SignalTimeout if hasattr(signal, 'SIGALRM') else GenericTimeout # type: ignore" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Synopsis\n", "\n", "The `Timeout` class throws a `TimeoutError` exception after a given timeout has expired.\n", "Its typical usage is in conjunction with a `with` clause:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:45.282384Z", "iopub.status.busy": "2023-11-12T13:02:45.282281Z", "iopub.status.idle": "2023-11-12T13:02:45.484382Z", "shell.execute_reply": "2023-11-12T13:02:45.484094Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Timeout!\n" ] } ], "source": [ "try:\n", " with Timeout(0.2):\n", " some_long_running_function()\n", " print(\"complete!\")\n", "except TimeoutError:\n", " print(\"Timeout!\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Note: On Unix/Linux systems, the `Timeout` class uses [`SIGALRM` signals](https://docs.python.org/3.10/library/signal.html) (interrupts) to implement timeouts; this has no effect on performance of the tracked code. On other systems (notably Windows), `Timeout` uses the [`sys.settrace()`](https://docs.python.org/3.10/library/sys.html?highlight=settrace#sys.settrace) function to check the timer after each line of code, which affects performance of the tracked code." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Create a `Timeout` variant that works efficiently on Windows. Note that how to do this a long debated issue in programming forums." ] } ], "metadata": { "ipub": { "bibliography": "fuzzingbook.bib", "toc": true }, "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true }, "toc-autonumbering": false, "vscode": { "interpreter": { "hash": "0af4f07dd039d1b4e562c7a7d0340393b1c66f50605ac6af30beb81aa23b7ef5" } } }, "nbformat": 4, "nbformat_minor": 4 }