OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """WatchdogTimer timeout objects.""" | 5 """WatchdogTimer timeout objects.""" |
6 | 6 |
7 import time | 7 import time |
8 | 8 |
9 | 9 |
10 class WatchdogTimer(object): | 10 class WatchdogTimer(object): |
11 """A resetable timeout-based watchdog. | 11 """A resetable timeout-based watchdog. |
12 | 12 |
13 This object is threadsafe. | 13 This object is threadsafe. |
14 """ | 14 """ |
15 | 15 |
16 def __init__(self, timeout): | 16 def __init__(self, timeout): |
17 """Initializes the watchdog. | 17 """Initializes the watchdog. |
18 | 18 |
19 Args: | 19 Args: |
20 timeout: The timeout in seconds. If timeout is None it will never timeout. | 20 timeout: The timeout in seconds. If timeout is None it will never timeout. |
21 """ | 21 """ |
22 self._start_time = time.time() | |
23 self._timeout = timeout | 22 self._timeout = timeout |
23 self._start_time = None | |
jbudorick
2014/10/30 02:27:03
This should still set self._start_time to time.tim
perezju
2014/10/30 18:44:45
Yeah, I scrapped much of this in the new version.
| |
24 self._end_time = None | |
25 self.Reset() | |
24 | 26 |
25 def Reset(self): | 27 def Reset(self): |
26 """Resets the timeout countdown.""" | 28 """Resets the timeout countdown.""" |
27 self._start_time = time.time() | 29 self._start_time = time.time() |
30 if self._timeout is None: | |
31 self._end_time = None | |
32 else: | |
33 self._end_time = self._start_time + self._timeout | |
34 | |
35 def ElapsedTime(self): | |
36 """Returns the number of seconds elapsed since the last reset.""" | |
37 return time.time() - self._start_time | |
38 | |
39 def RemainingTime(self): | |
40 """Returns the number of seconds remaining until the watchdog times out.""" | |
41 if self._end_time is None: | |
42 return None | |
43 else: | |
44 return self._end_time - time.time() | |
28 | 45 |
29 def IsTimedOut(self): | 46 def IsTimedOut(self): |
30 """Whether the watchdog has timed out. | 47 """Whether the watchdog has timed out. |
31 | 48 |
32 Returns: | 49 Returns: |
33 True if the watchdog has timed out, False otherwise. | 50 True if the watchdog has timed out, False otherwise. |
34 """ | 51 """ |
35 if self._timeout is None: | 52 if self._end_time is None: |
36 return False | 53 return False |
37 return time.time() - self._start_time > self._timeout | 54 else: |
55 return time.time() > self._end_time | |
OLD | NEW |