import threading


def with_timeout(*args, **kwargs):
    """
    Exec function with the given timeout

    Args:
        timeout:
            Timeout in seconds.
        func:
            Function to be executed.

    Examples:
        >>> with_timeout(5, sum, [1, 2, 3])
        6
    """
    timeout, func, *args = args
    result_container = []

    def wrapped():
        result_container.append(func(*args, **kwargs))

    thread = threading.Thread(target=wrapped)
    thread.start()
    thread.join(timeout)
    if result_container:
        return result_container[0]
    raise ValueError('function could not produce output')