Robustly index an array, using retry logic with exponential backoff if any of the errors ``catch`` are raised. The initial_delay is measured in ms. With the default settings, the maximum delay will be in the range of 32-64 seconds.
(array, key, catch=Exception, max_retries=6, initial_delay=500)
| 287 | |
| 288 | |
| 289 | def robust_getitem(array, key, catch=Exception, max_retries=6, initial_delay=500): |
| 290 | """ |
| 291 | Robustly index an array, using retry logic with exponential backoff if any |
| 292 | of the errors ``catch`` are raised. The initial_delay is measured in ms. |
| 293 | |
| 294 | With the default settings, the maximum delay will be in the range of 32-64 |
| 295 | seconds. |
| 296 | """ |
| 297 | assert max_retries >= 0 |
| 298 | for n in range(max_retries + 1): |
| 299 | try: |
| 300 | return array[key] |
| 301 | except catch: |
| 302 | if n == max_retries: |
| 303 | raise |
| 304 | base_delay = initial_delay * 2**n |
| 305 | next_delay = base_delay + np.random.randint(base_delay) |
| 306 | msg = ( |
| 307 | f"getitem failed, waiting {next_delay} ms before trying again " |
| 308 | f"({max_retries - n} tries remaining). Full traceback: {traceback.format_exc()}" |
| 309 | ) |
| 310 | logger.debug(msg) |
| 311 | time.sleep(1e-3 * next_delay) |
| 312 | |
| 313 | |
| 314 | class BackendArray(NdimSizeLenMixin, indexing.ExplicitlyIndexed): |
no outgoing calls
searching dependent graphs…