You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implements an improved task polling mechanism with configurable wait
time. This helps to minimize resource consumption on the Connect server
while still providing responsive feedback.
Resolves#184
Initial wait time in seconds. First API request will use this as the wait parameter.
107
-
max_wait : int, default 10
103
+
wait : int, default 1
108
104
Maximum wait time in seconds between polling requests.
109
-
backoff : float, default 1.5
110
-
Backoff multiplier for increasing wait times.
105
+
max_attempts : int | None, default None
106
+
Maximum number of polling attempts. If None, polling will continue indefinitely.
107
+
108
+
Raises
109
+
------
110
+
TimeoutError
111
+
If the task does not finish within the maximum attempts.
112
+
113
+
Notes
114
+
-----
115
+
If the task finishes before the wait time or maximum attempts are reached, the function will return immediately. For example, if the wait time is set to 5 seconds and the task finishes in 2 seconds, the function will return after 2 seconds.
116
+
117
+
If the task does not finished after the maximum attempts, a TimeoutError will be raised. By default, the maximum attempts is None, which means the function will wait indefinitely until the task finishes.
111
118
112
119
Examples
113
120
--------
114
121
>>> task.wait_for()
115
122
None
116
123
117
-
Notes
118
-
-----
119
-
This method implements an exponential backoff strategy to reduce the number of API calls
120
-
while waiting for long-running tasks. The first request uses the initial_wait value,
121
-
and subsequent requests increase the wait time by the backoff factor, up to max_wait. To disable exponential backoff, set backoff to 1.0.
122
-
"""
123
-
wait_time=initial_wait
124
+
Waiting for a task to finish with a custom wait time.
124
125
125
-
whilenotself.is_finished:
126
-
self.update()
126
+
>>> task.wait_for(wait=5)
127
+
None
127
128
128
-
# Wait client-side
129
-
time.sleep(wait_time)
129
+
Waiting for a task with a maximum number of attempts.
130
130
131
-
# Calculate next wait time with backoff
132
-
wait_time=min(wait_time*backoff, max_wait)
131
+
>>> task.wait_for(max_attempts=3)
132
+
None
133
+
"""
134
+
attempts=0
135
+
whilenotself.is_finished:
136
+
ifmax_attemptsisnotNoneandattempts>=max_attempts:
137
+
break
138
+
self.update(wait=wait)
139
+
attempts+=1
140
+
141
+
ifnotself.is_finished:
142
+
raiseTimeoutError(
143
+
f"Task {self['id']} did not finish within the specified wait time or maximum attempts."
0 commit comments