MCPcopy Index your code
hub / github.com/pydata/xarray / robust_getitem

Function robust_getitem

xarray/backends/common.py:289–311  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

287
288
289def 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
314class BackendArray(NdimSizeLenMixin, indexing.ExplicitlyIndexed):

Callers 3

_getitemMethod · 0.90
test_robust_getitemFunction · 0.90
test_robust_getitemMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_robust_getitemFunction · 0.72
test_robust_getitemMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…