{"id":7759,"library":"stopit","title":"stopit: Timeout Control for Python","description":"stopit is a Python library that provides flexible mechanisms for controlling the execution time of code blocks and functions. It offers both context managers and decorators to enforce timeouts and can raise exceptions in other threads. The current version is 1.1.2, with its last major update in 2018, indicating a maintenance-focused release cadence.","status":"maintenance","version":"1.1.2","language":"python","source_language":"en","source_url":"https://github.com/glenfant/stopit","tags":["timeout","context-manager","decorator","threading","exception","control-flow"],"install":[{"cmd":"pip install stopit","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"wrong":"from stopit import ThreadingTimeout","symbol":"ThreadingTimeout","correct":"from stopit import ThreadingTimeout"}],"quickstart":{"code":"import stopit\nimport time\n\nprint(\"Starting long running task with 5-second timeout...\")\nwith stopit.ThreadingTimeout(5) as context_manager:\n    # Simulate a long-running operation\n    for i in range(10**8):\n        _ = i * 2 # Some computation\n        if not context_manager.state == context_manager.EXECUTING: # Optional: check state inside loop\n            break\n\nif context_manager.state == context_manager.EXECUTED:\n    print(\"Task completed within timeout.\")\nelif context_manager.state == context_manager.TIMED_OUT:\n    print(\"Task timed out after 5 seconds.\")\n\n# Example using a decorator\n@stopit.threading_timeoutable(default='Timed out!')\ndef potentially_long_function(timeout=1, max_iterations=10**8):\n    print(f\"  Function starting with {timeout}s timeout...\")\n    for i in range(max_iterations):\n        _ = i * 2\n        time.sleep(0.001) # Small sleep to allow thread context switching\n    return \"Function completed.\"\n\nresult = potentially_long_function(timeout=2)\nprint(f\"Decorator result: {result}\")\n\nresult_fast = potentially_long_function(timeout=10, max_iterations=1000)\nprint(f\"Decorator result (fast completion): {result_fast}\")","lang":"python","description":"This quickstart demonstrates using `stopit.ThreadingTimeout` as a context manager and `stopit.threading_timeoutable` as a decorator to limit the execution time of code. The context manager allows checking its state to determine if execution completed or timed out. The decorator automatically returns a default value if the function exceeds its allowed time."},"warnings":[{"fix":"Update your imports and usage to `ThreadingTimeout` and `threading_timeoutable` respectively.","message":"In version 1.1.0, the main context manager `stopit.Timeout` was renamed to `stopit.ThreadingTimeout`, and the decorator `stopit.timeoutable` was renamed to `stopit.threading_timeoutable`. Using the old names will result in import errors or unexpected behavior.","severity":"breaking","affected_versions":"1.0.0 and earlier"},{"fix":"Use the threading-based context managers (`ThreadingTimeout`) and decorators (`threading_timeoutable`) which are platform-independent for basic timeout functionality.","message":"Signal-based timeout controls (`SignalTimeout` and `signal_timeoutable`) are not supported on Windows due to the operating system's lack of `signal.SIGALRM` support.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your application is running on CPython. Be aware that `stopit` might not be suitable for projects requiring broad Python interpreter compatibility.","message":"Threading-based timeout mechanisms (like `ThreadingTimeout`) will only work reliably with CPython implementations. They rely on CPython-specific low-level APIs and are not compatible with alternative Python implementations like PyPy, Jython, or IronPython.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For very precise or immediate interruption of blocking I/O or long `time.sleep` calls, consider using `multiprocessing` to run the task in a separate process that can be terminated.","message":"The library cannot stop the execution of blocking Python atomic instructions that acquire the Global Interpreter Lock (GIL), such as `time.sleep()` for long durations or I/O operations. The asynchronous exception will only be effective *after* such blocking operations complete.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid setting extremely small or zero timeout values. If precise, sub-millisecond control is critical, `stopit` might not be the ideal solution due to Python's GIL and thread scheduling characteristics.","message":"Very short timeout values, especially zero, can lead to unexpected `TimeoutException` being raised immediately or trigger race conditions, particularly on Windows/Cygwin. The accuracy of timeouts generally depends on the GIL interval checking of your Python platform.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.1.2':46 '2018':53 'block':22 'cadenc':60 'code':21 'context':28,63 'context-manag':62 'control':3,16,69 'control-flow':68 'current':43 'decor':31,65 'enforc':33 'except':38,67 'execut':18 'flexibl':13 'flow':70 'focus':58 'function':24 'indic':54 'last':49 'librari':10 'mainten':57 'maintenance-focus':56 'major':50 'manag':29,64 'mechan':14 'offer':26 'provid':12 'python':5,9 'rais':37 'releas':59 'stopit':1,6 'thread':41,66 'time':19 'timeout':2,34,61 'updat':51 'version':44","created_at":"2026-04-16T14:12:10.062765+00:00","updated_at":"2026-04-16T14:12:10.062765+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\n  File \"/tmp/tmp8vu7nui7/venv/lib/python3.12/site-packages/stopit/__init__.py\", line 10, in <module>\n    import pkg_resources\nModuleNotFoundError: No module named 'pkg_resources'"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.1.2","cli_name":"","cli_version":null,"type":"library","homepage":"http://pypi.python.org/pypi/stopit","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/stopit/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"import_fail","verified_at":"2026-07-03","last_verified":"2026-07-03","next_check":"2026-07-10","install_tag":null}}