Skip to content

Commit 972cca8

Browse files
authored
Update timeout.py
1 parent 3602ffc commit 972cca8

File tree

1 file changed

+8
-35
lines changed

1 file changed

+8
-35
lines changed

tests/integ/timeout.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,25 @@
1212
# language governing permissions and limitations under the License.
1313
from __future__ import absolute_import
1414

15-
import signal
1615
from contextlib import contextmanager
1716
import logging
1817
from time import sleep
1918

2019
from awslogs.core import AWSLogs
2120
from botocore.exceptions import ClientError
21+
import stopit
2222

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
4523

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")
5625

5726

5827
@contextmanager
5928
def timeout_and_delete_endpoint_by_name(
6029
endpoint_name, sagemaker_session, seconds=0, minutes=45, hours=0
6130
):
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:
6334
no_errors = False
6435
try:
6536
yield [t]
@@ -89,7 +60,9 @@ def timeout_and_delete_endpoint_by_name(
8960
def timeout_and_delete_model_with_transformer(
9061
transformer, sagemaker_session, seconds=0, minutes=0, hours=0
9162
):
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:
9366
no_errors = False
9467
try:
9568
yield [t]

0 commit comments

Comments
 (0)