-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathutils.py
More file actions
34 lines (31 loc) · 1.02 KB
/
utils.py
File metadata and controls
34 lines (31 loc) · 1.02 KB
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
31
32
33
34
import time
from typing import Iterable, Optional
from dstack._internal.core.models.runs import Run
from dstack.api.server import APIClient
def poll_run(
api_client: APIClient,
project_name: str,
run_name: str,
delay: float = 5.0,
timeout: Optional[float] = None,
) -> Iterable[Run]:
"""
Polls run every `delay` seconds until `timeout` is reached (if any).
:param api_client: Dstack server APIClient
:param project_name: Project name
:param run_name: Run name
:param delay: How often to poll run, default is 5 seconds
:param timeout: Timeout in seconds, default is None (no timeout)
:yield: Run model
"""
start_time = time.monotonic()
while True:
run = api_client.runs.get(project_name, run_name)
yield run
now = time.monotonic()
sleep = delay
if timeout is not None:
if now > start_time + timeout:
raise TimeoutError()
sleep = min(sleep, start_time + timeout - now)
time.sleep(sleep)