Skip to content

Commit ac0485b

Browse files
authored
Update timeout.py
1 parent 3884cc2 commit ac0485b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/integ/timeout.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@
2424
LOGGER = logging.getLogger("timeout")
2525

2626

27+
@contextmanager
28+
def timeout(seconds=0, minutes=0, hours=0):
29+
"""
30+
Add a signal-based timeout to any block of code.
31+
If multiple time units are specified, they will be added together to determine time limit.
32+
Usage:
33+
with timeout(seconds=5):
34+
my_slow_function(...)
35+
Args:
36+
- seconds: The time limit, in seconds.
37+
- minutes: The time limit, in minutes.
38+
- hours: The time limit, in hours.
39+
"""
40+
41+
limit = seconds + 60 * minutes + 3600 * hours
42+
43+
with stopit.ThreadingTimeout(limit, swallow_exc=False) as t:
44+
yield [t]
45+
46+
2747
@contextmanager
2848
def timeout_and_delete_endpoint_by_name(
2949
endpoint_name, sagemaker_session, seconds=0, minutes=45, hours=0

0 commit comments

Comments
 (0)