{"id":2956,"library":"func-timeout","title":"func-timeout","description":"`func-timeout` is a Python module that enables setting timeouts for arbitrary function calls, preventing them from running indefinitely. It achieves this by executing the function in a separate thread and forcefully terminating it if the specified duration is exceeded. The latest version is 4.3.6, with a historically sporadic release cadence, recently updated to support newer Python versions.","status":"active","version":"4.3.6","language":"en","source_language":"en","source_url":"https://github.com/kata198/func_timeout","tags":["timeout","thread","function","decorator"],"install":[{"cmd":"pip install func-timeout","lang":"bash","label":"Install func-timeout"}],"dependencies":[],"imports":[{"symbol":"func_timeout","correct":"from func_timeout import func_timeout"},{"symbol":"func_set_timeout","correct":"from func_timeout import func_set_timeout"},{"symbol":"FunctionTimedOut","correct":"from func_timeout import FunctionTimedOut"}],"quickstart":{"code":"import time\nfrom func_timeout import func_timeout, FunctionTimedOut, func_set_timeout\n\ndef long_running_function(duration):\n    print(f\"Starting long_running_function for {duration} seconds...\")\n    time.sleep(duration)\n    print(\"long_running_function completed.\")\n    return \"Done\"\n\n# Example 1: Using func_timeout for a direct call\ntry:\n    print(\"\\n--- Using func_timeout ---\")\n    result = func_timeout(1, long_running_function, args=(5,))\n    print(f\"Result: {result}\")\nexcept FunctionTimedOut:\n    print(\"Function timed out after 1 second!\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n\n# Example 2: Using func_set_timeout decorator\n@func_set_timeout(1)\ndef decorated_function(duration):\n    print(f\"Starting decorated_function for {duration} seconds...\")\n    time.sleep(duration)\n    print(\"decorated_function completed.\")\n    return \"Decorated Done\"\n\ntry:\n    print(\"\\n--- Using func_set_timeout decorator ---\")\n    result = decorated_function(5)\n    print(f\"Result from decorator: {result}\")\nexcept FunctionTimedOut:\n    print(\"Decorated function timed out after 1 second!\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to use both the `func_timeout` function and the `func_set_timeout` decorator to apply timeouts to Python functions. It shows how `FunctionTimedOut` is raised when the timeout is exceeded."},"warnings":[{"fix":"Design functions to be interruptible, use cooperative cancellation (e.g., checking a flag periodically), or be acutely aware of potential side effects and design your application to tolerate them. Avoid `func-timeout` for functions performing critical operations or managing external resources without explicit cleanup.","message":"Thread Termination Safety: `func-timeout` achieves timeouts by forcefully terminating a separate thread. This is inherently unsafe in Python, as threads cannot be reliably or gracefully stopped. This can lead to resource leaks (e.g., open files, network connections), corrupted shared state, or deadlocks if the timed-out function was holding locks or critical resources.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you are using version 4.1.0 or newer for improved traceback clarity and more robust thread cleanup.","message":"Unpredictable Tracebacks and Cleanup: In versions prior to 4.1.0, tracebacks might have been less clear, and explicit thread cleanup might not have occurred reliably, potentially leading to zombie threads or resource issues. Version 4.0.0 also fixed issues where tracebacks could be printed to stderr outside the normal exception handling.","severity":"gotcha","affected_versions":"< 4.1.0 (especially < 4.0.0 for traceback issues)"},{"fix":"Upgrade to version 4.3.0 or newer, which uses `functools.wraps` to preserve function metadata.","message":"Decorator Metadata Loss (pre-4.3.0): When using the `@func_set_timeout` decorator in versions prior to 4.3.0, the decorated function might lose its original metadata (e.g., `__name__`, `__doc__`). This can affect introspection tools or other decorators in a chain.","severity":"gotcha","affected_versions":"< 4.3.0"},{"fix":"Always use the latest available version of `func-timeout` (currently 4.3.6) to ensure compatibility and leverage internal fixes for newer Python versions.","message":"Python Version Compatibility: Older versions of `func-timeout` might use deprecated threading API calls (e.g., `isA` instead of `is_alive` in versions before 4.3.4) or have less robust support for newer Python interpreters.","severity":"gotcha","affected_versions":"< 4.3.4 (for deprecated `isA`), potentially older versions for broader Python 3.x support"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}