32 lines · python
1# DExTer : Debugging Experience Tester2# ~~~~~~ ~ ~~ ~ ~~3#4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5# See https://llvm.org/LICENSE.txt for license information.6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7"""Utility class to check for timeouts. Timer starts when the object is initialized,8and can be checked by calling timed_out(). Passing a timeout value of 0.0 or less9means a timeout will never be triggered, i.e. timed_out() will always return False.10"""11 12import time13 14 15class Timeout(object):16 def __init__(self, duration: float):17 self.start = self.now18 self.duration = duration19 20 def timed_out(self):21 if self.duration <= 0.0:22 return False23 return self.elapsed > self.duration24 25 @property26 def elapsed(self):27 return self.now - self.start28 29 @property30 def now(self):31 return time.time()32