Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry if call function on backend fails #242

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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