Ref : https://github.com/python/cpython/pull/25174

https://github.com/localstack/localstack/blob/5a5edcd9d9ccbd6a56f23d71e3a1131b8c814996/localstack/utils/common.py#L324
return run_safe(run, print_error=False, **kwargs)
def run_for_max_seconds(max_secs, _function, *args, **kwargs):
    """ Run the given function for a maximum of `max_secs` seconds - continue running
        in a background thread if the function does not finish in time. """
    def _worker(*_args):
        result = None
        try:
            result = _function(*args, **kwargs)
        except Exception as e:
            result = e
        result = True if result is None else result
        q.put(result)
        return result
    start = now()
    q = Queue()
    start_worker_thread(_worker)
    for i in range(max_secs * 2):
        result = None
        try:
            result = q.get_nowait()
        except Exception:
            pass
        if result is not None:
            if isinstance(result, Exception):
                raise result
            return result
        if now() - start >= max_secs:
            return
        time.sleep(0.5)
def run(cmd, cache_duration_secs=0, **kwargs):