-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtimeout.py
More file actions
30 lines (24 loc) · 772 Bytes
/
timeout.py
File metadata and controls
30 lines (24 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import time
from typing import Callable
def timeout(milliseconds: int, callable: Callable[[], None]) -> None:
start = time.monotonic()
end = start + milliseconds / 1000
while time.monotonic() <= end:
try:
callable()
except AssertionError:
time.sleep(0.05)
else:
return
raise TimeoutError("Timeout reached")
def timeout_never(milliseconds: int, callable: Callable[[], None]) -> None:
__tracebackhide__ = True
start = time.monotonic()
end = start + milliseconds / 1000
while time.monotonic() <= end:
try:
callable()
except AssertionError:
time.sleep(0.05)
else:
raise AssertionError("Condition should never be met")