{"id":22461,"library":"timeout-sampler","title":"timeout-sampler","description":"A utility class that repeatedly calls a function until it returns a truthy value or a timeout expires. Provides customizable retry logic with sleep intervals. Version 1.0.22, actively maintained, monthly releases.","status":"active","version":"1.0.22","language":"python","source_language":"en","source_url":"https://github.com/RedHatQE/timeout-sampler","tags":["timeout","retry","sampler","utility"],"install":[{"cmd":"pip install timeout-sampler","lang":"bash","label":"pip"}],"dependencies":[],"imports":[{"note":"Class is CamelCase, not snake_case","wrong":"from timeout_sampler import timeout_sampler","symbol":"TimeoutSampler","correct":"from timeout_sampler import TimeoutSampler"},{"note":"Exception is exported from top-level package","wrong":"from timeout_sampler.exceptions import TimeoutExpiredError","symbol":"TimeoutExpiredError","correct":"from timeout_sampler import TimeoutExpiredError"}],"quickstart":{"code":"from timeout_sampler import TimeoutSampler\nimport time\n\ndef sample_func():\n    return None  # simulate condition not ready\n\n# Wait up to 5 seconds, check every 0.5 seconds\nsampler = TimeoutSampler(\n    timeout=5,\n    sleep=0.5,\n    func=sample_func,\n    exceptions_dict={}\n)\ntry:\n    for value in sampler:\n        if value:\n            print(\"Condition met\")\n            break\nexcept TimeoutExpiredError:\n    print(\"Timeout reached\")","lang":"python","description":"Basic usage showing how to wait for a function to return a truthy value."},"warnings":[{"fix":"Break out of the for loop once condition is met.","message":"Iteration does not stop on first truthy value automatically; you must break yourself or use a loop with break. The iterator yields all results until timeout unless you break early.","severity":"gotcha","affected_versions":"all"},{"fix":"Pass a dictionary mapping exception types to booleans (True to retry, False to raise) or an empty dict {} to avoid retry on any exception.","message":"The 'exceptions_dict' parameter is required if you want to retry on exceptions; if omitted or empty, the sampler will not catch exceptions and they will propagate immediately.","severity":"gotcha","affected_versions":"all"},{"fix":"Use 'from timeout_sampler import TimeoutSampler'.","message":"Import from 'timeout_sampler' directly is the only supported path. Some older examples incorrectly import from submodules like 'timeout_sampler.timeout_sampler'.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"pip uninstall timeout-sampler-python && pip install timeout-sampler","cause":"Installed incorrect package 'timeout-sampler-python' or similar name. The correct package is 'timeout-sampler'.","error":"AttributeError: module 'timeout_sampler' has no attribute 'TimeoutSampler'"},{"fix":"Add exceptions_dict={} or a dict with exception->bool mapping.","cause":"The 'exceptions_dict' parameter is mandatory in constructor.","error":"TypeError: __init__() missing 1 required positional argument: 'exceptions_dict'"},{"fix":"Wrap iteration in try/except TimeoutExpiredError.","cause":"The iterator raises StopIteration if timeout expires before any truthy value is returned. Usually handled by catching TimeoutExpiredError.","error":"StopIteration"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}