|
12 | 12 | # language governing permissions and limitations under the License.
|
13 | 13 | from __future__ import absolute_import
|
14 | 14 |
|
15 |
| -import signal |
16 | 15 | from contextlib import contextmanager
|
17 | 16 | import logging
|
18 | 17 | from time import sleep
|
19 | 18 |
|
20 | 19 | from awslogs.core import AWSLogs
|
21 | 20 | from botocore.exceptions import ClientError
|
| 21 | +import stopit |
22 | 22 |
|
23 |
| -LOGGER = logging.getLogger("timeout") |
24 |
| - |
25 |
| - |
26 |
| -class TimeoutError(Exception): |
27 |
| - pass |
28 |
| - |
29 |
| - |
30 |
| -@contextmanager |
31 |
| -def timeout(seconds=0, minutes=0, hours=0): |
32 |
| - """ |
33 |
| - Add a signal-based timeout to any block of code. |
34 |
| - If multiple time units are specified, they will be added together to determine time limit. |
35 |
| - Usage: |
36 |
| - with timeout(seconds=5): |
37 |
| - my_slow_function(...) |
38 |
| - Args: |
39 |
| - - seconds: The time limit, in seconds. |
40 |
| - - minutes: The time limit, in minutes. |
41 |
| - - hours: The time limit, in hours. |
42 |
| - """ |
43 |
| - |
44 |
| - limit = seconds + 60 * minutes + 3600 * hours |
45 | 23 |
|
46 |
| - def handler(signum, frame): |
47 |
| - raise TimeoutError("timed out after {} seconds".format(limit)) |
48 |
| - |
49 |
| - try: |
50 |
| - signal.signal(signal.SIGALRM, handler) |
51 |
| - signal.alarm(limit) |
52 |
| - |
53 |
| - yield |
54 |
| - finally: |
55 |
| - signal.alarm(0) |
| 24 | +LOGGER = logging.getLogger("timeout") |
56 | 25 |
|
57 | 26 |
|
58 | 27 | @contextmanager
|
59 | 28 | def timeout_and_delete_endpoint_by_name(
|
60 | 29 | endpoint_name, sagemaker_session, seconds=0, minutes=45, hours=0
|
61 | 30 | ):
|
62 |
| - with timeout(seconds=seconds, minutes=minutes, hours=hours) as t: |
| 31 | + limit = seconds + 60 * minutes + 3600 * hours |
| 32 | + |
| 33 | + with stopit.ThreadingTimeout(limit, swallow_exc=False) as t: |
63 | 34 | no_errors = False
|
64 | 35 | try:
|
65 | 36 | yield [t]
|
@@ -89,7 +60,9 @@ def timeout_and_delete_endpoint_by_name(
|
89 | 60 | def timeout_and_delete_model_with_transformer(
|
90 | 61 | transformer, sagemaker_session, seconds=0, minutes=0, hours=0
|
91 | 62 | ):
|
92 |
| - with timeout(seconds=seconds, minutes=minutes, hours=hours) as t: |
| 63 | + limit = seconds + 60 * minutes + 3600 * hours |
| 64 | + |
| 65 | + with stopit.ThreadingTimeout(limit, swallow_exc=False) as t: |
93 | 66 | no_errors = False
|
94 | 67 | try:
|
95 | 68 | yield [t]
|
|
0 commit comments