d9bf9f0a40
* enable some more flake8 checks * some more quick ones * bump opendbc * e401 * e711 e712 * e115 e116 * e222 * e301 * remove that * e129 * e701 e702 * e125 e131 * e227 * e306 * e262 * W503 * e713 * e704 * e731 * bump opendbc * fix some e722
28 lines
707 B
Python
28 lines
707 B
Python
import signal
|
|
|
|
class TimeoutException(Exception):
|
|
pass
|
|
|
|
class Timeout:
|
|
"""
|
|
Timeout context manager.
|
|
For example this code will raise a TimeoutException:
|
|
with Timeout(seconds=5, error_msg="Sleep was too long"):
|
|
time.sleep(10)
|
|
"""
|
|
def __init__(self, seconds, error_msg=None):
|
|
if error_msg is None:
|
|
error_msg = 'Timed out after {} seconds'.format(seconds)
|
|
self.seconds = seconds
|
|
self.error_msg = error_msg
|
|
|
|
def handle_timeout(self, signume, frame):
|
|
raise TimeoutException(self.error_msg)
|
|
|
|
def __enter__(self):
|
|
signal.signal(signal.SIGALRM, self.handle_timeout)
|
|
signal.alarm(self.seconds)
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
signal.alarm(0)
|