Polling Module¶
This section documents the polling components of the Nextmv Python SDK.
polling
¶
Polling module containing logic to poll for a run result.
Polling can be used with both Cloud and local applications.
CLASS | DESCRIPTION |
---|---|
PollingOptions |
Options to use when polling for a run result. |
FUNCTION | DESCRIPTION |
---|---|
poll |
Function to poll a function until it succeeds or the polling strategy is exhausted. |
DEFAULT_POLLING_OPTIONS
module-attribute
¶
DEFAULT_POLLING_OPTIONS: PollingOptions = PollingOptions()
Default polling options to use when polling for a run result. This constant
provides the default values for PollingOptions
used across the module.
Using these defaults is recommended for most use cases unless specific timing
needs are required.
PollingOptions
dataclass
¶
PollingOptions(
backoff: float = 0.9,
delay: float = 0.1,
initial_delay: float = 1,
max_delay: float = 20,
max_duration: float = -1,
max_tries: int = -1,
jitter: float = 1,
verbose: bool = False,
stop: Optional[Callable[[], bool]] = None,
)
Options to use when polling for a run result.
You can import the PollingOptions
class directly from nextmv
:
The Cloud API will be polled for the result. The polling stops if:
- The maximum number of polls (tries) are exhausted. This is specified by
the
max_tries
parameter. - The maximum duration of the polling strategy is reached. This is
specified by the
max_duration
parameter.
Before conducting the first poll, the initial_delay
is used to sleep.
After each poll, a sleep duration is calculated using the following
strategy, based on exponential backoff with jitter:
Where: * i is the retry (poll) number. * Uniform is the uniform distribution.
Note that the sleep duration is capped by the max_delay
parameter.
PARAMETER | DESCRIPTION |
---|---|
|
Exponential backoff factor, in seconds, to use between polls.
TYPE:
|
|
Base delay to use between polls, in seconds.
TYPE:
|
|
Initial delay to use before starting the polling strategy, in seconds.
TYPE:
|
|
Maximum delay to use between polls, in seconds.
TYPE:
|
|
Maximum duration of the polling strategy, in seconds.
TYPE:
|
|
Maximum number of tries to use.
TYPE:
|
|
Jitter to use for the polling strategy. A uniform distribution is sampled between 0 and this number. The resulting random number is added to the delay for each poll, adding a random noise. Set this to 0 to avoid using random jitter.
TYPE:
|
|
Whether to log the polling strategy. This is useful for debugging.
TYPE:
|
|
Function to call to check if the polling should stop. This is useful for stopping the polling based on external conditions. The function should return True to stop the polling and False to continue. The function does not receive any arguments. The function is called before each poll.
TYPE:
|
Examples:
>>> from nextmv.cloud import PollingOptions
>>> # Create polling options with custom settings
>>> polling_options = PollingOptions(
... max_tries=50,
... max_duration=600,
... verbose=True
... )
backoff
class-attribute
instance-attribute
¶
Exponential backoff factor, in seconds, to use between polls.
delay
class-attribute
instance-attribute
¶
Base delay to use between polls, in seconds.
initial_delay
class-attribute
instance-attribute
¶
Initial delay to use before starting the polling strategy, in seconds.
jitter
class-attribute
instance-attribute
¶
Jitter to use for the polling strategy. A uniform distribution is sampled between 0 and this number. The resulting random number is added to the delay for each poll, adding a random noise. Set this to 0 to avoid using random jitter.
max_delay
class-attribute
instance-attribute
¶
Maximum delay to use between polls, in seconds.
max_duration
class-attribute
instance-attribute
¶
Maximum duration of the polling strategy, in seconds. A negative value means no limit.
max_tries
class-attribute
instance-attribute
¶
Maximum number of tries to use. A negative value means no limit.
stop
class-attribute
instance-attribute
¶
Function to call to check if the polling should stop. This is useful for stopping the polling based on external conditions. The function should return True to stop the polling and False to continue. The function does not receive any arguments. The function is called before each poll.
verbose
class-attribute
instance-attribute
¶
Whether to log the polling strategy. This is useful for debugging.
poll
¶
poll(
polling_options: PollingOptions,
polling_func: Callable[[], tuple[Any, bool]],
__sleep_func: Callable[[float], None] = sleep,
) -> Any
Poll a function until it succeeds or the polling strategy is exhausted.
You can import the poll
function directly from nextmv
:
This function implements a flexible polling strategy with exponential backoff and jitter. It calls the provided polling function repeatedly until it indicates success, the maximum number of tries is reached, or the maximum duration is exceeded.
The polling_func
is a callable that must return a tuple[Any, bool]
where the first element is the result of the polling and the second
element is a boolean indicating if the polling was successful or should be
retried.
PARAMETER | DESCRIPTION |
---|---|
|
Options for configuring the polling behavior, including retry counts, delays, timeouts, and verbosity settings.
TYPE:
|
|
Function to call to check if the polling was successful. Must return a tuple where the first element is the result value and the second is a boolean indicating success (True) or need to retry (False).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
Result value from the polling function when successful. |
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the polling exceeds the maximum duration specified in polling_options. |
RuntimeError
|
If the maximum number of tries is exhausted without success. |
Examples:
>>> from nextmv.cloud import PollingOptions, poll
>>> import time
>>>
>>> # Define a polling function that succeeds after 3 tries
>>> counter = 0
>>> def check_completion() -> tuple[str, bool]:
... global counter
... counter += 1
... if counter >= 3:
... return "Success", True
... return None, False
...
>>> # Configure polling options
>>> options = PollingOptions(
... max_tries=5,
... delay=0.1,
... backoff=0.2,
... verbose=True
... )
>>>
>>> # Poll until the function succeeds
>>> result = poll(options, check_completion)
>>> print(result)
'Success'
Source code in nextmv/nextmv/polling.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|