Skip to content

Commit

Permalink
Retry if call function on backend fails (#242)
Browse files Browse the repository at this point in the history
* Retry if call function on backend fails

* Improve code and error chaining

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
hagenw and sourcery-ai[bot] authored Nov 18, 2024
1 parent 2647892 commit 8da41a4
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 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,40 @@ 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
if retry + 1 == retries:
raise BackendError(ex) from ex
time.sleep(0.05)


def check_path(
Expand Down

0 comments on commit 8da41a4

Please sign in to comment.