class ThreadedRunner(Runnable): def __init__(self, runnable): self._runnable = runnable self._notifier = Event() self._result = None self._error = None self._traceback = None self._thread = None def run(self): try: self._result = self._runnable() except: self._error, self._traceback = sys.exc_info()[1:] self._notifier.set() __call__ = run def run_in_thread(self, timeout): self._thread = Thread(self, name=TIMEOUT_THREAD_NAME) self._thread.setDaemon(True) self._thread.start() self._notifier.wait(timeout) return self._notifier.isSet() def get_result(self): if self._error: raise self._error, None, self._traceback return self._result def stop_thread(self): self._thread.stop()
def execute(self, runnable): runner = Runner(runnable) thread = Thread(runner, name='RobotFrameworkTimeoutThread') thread.setDaemon(True) thread.start() thread.join(int(self._timeout * 1000)) if thread.isAlive(): thread.stop() raise TimeoutError(self._error) return runner.get_result()