Exemplo n.º 1
0
    def run(self, func):
        """
        Invokes func with a specified timeout_seconds.

        If func takes more than timeout_seconds seconds to run,
        TimeoutException is raised.
        If timeout_seconds == 0, it simply calls the function.

        :return: whatever func returns
        """
        if self._timeout_seconds == 0:
            # Timeout is disabled.
            return self._timed_call(func)
        else:
            try:
                with futures.ThreadPoolExecutor(max_workers=1) as executor:
                    future = executor.submit(self._timed_call, func)
                    return future.result(timeout=self._timeout_seconds)
            except futures.TimeoutError:
                raise TimeoutException(self._timeout_seconds)
Exemplo n.º 2
0
def alarm_handler(signum, frame):
    error_message = "Scoring timeout after {} ms".format(
        main.scoring_timeout_in_ms)
    raise TimeoutException(error_message)