import queue
import threading


def execute_function_with_timeout(fun, timeout=2):
    """
    Execute a function w/o argument (or pre filled with partial)
    and return result in less than `timeout` seconds, else raise Exception

    :param fun: function w/o argument (or pre filled w partial) to execute
    :param timeout: number of seconds allowed for the function execution
    """
    que = queue.Queue()
    thr = threading.Thread(target=lambda q: q.put(fun()), args=(que,))
    thr.start()
    thr.join(timeout)

    if que.empty():
        raise Exception("Timeout")
    else:
        return que.get()