Skip to content

Commit

Permalink
Retry if call function on backend fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Nov 14, 2024
1 parent 1d0c713 commit 8be6b93
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions audbackend/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import errno
import os
import re
import time

import audeer

Expand All @@ -23,15 +24,41 @@ def call_function_on_backend(
*args,
suppress_backend_errors: bool = False,
fallback_return_value: object = None,
retries: int = 3,
**kwargs,
) -> object:
try:
return function(*args, **kwargs)
except Exception as ex:
if suppress_backend_errors:
return fallback_return_value
else:
raise BackendError(ex)
r"""Call function on backend.
Args:
function: function to call on backend
suppress_backend_errors: if ``True``
and ``function`` fails during execution,
``fallback_return_value`` is returned
instead of raising an error
fallback_return_value: value returned
if ``function`` fails during execution
and ``suppress_backend_errors`` is ``True``
retries: number of times ``function``
is tried to execute
when it raises an error,
before raising the error
*args: positional args of ``function``
**kwargs: keyword arguments of ``function``
Returns:
return value(s) of ``function``
"""
for retry in range(retries):
try:
return function(*args, **kwargs)
except Exception as ex:
if suppress_backend_errors:
return fallback_return_value
else:
if retry + 1 == retries:
raise BackendError(ex)
time.sleep(0.05)


def check_path(
Expand Down

0 comments on commit 8be6b93

Please sign in to comment.