-
-
Notifications
You must be signed in to change notification settings - Fork 291
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
Add optional label to BaseRetrying class #418
base: main
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of adding a custom field in the retrying code just for the logging code.
I'm also unsure about what that solves exactly?
Current code assumes that there is always a function name available (there even was a comment saying that this should never happen) -- which is not the case when using a context manager. Currently logging functions would output "Retrying <unknown>" and there is no way to specify which section of code failed and needs the retry. The idea is to specify an optional label for a retry block. And if retry block is repeated in a loop it also allows to specify which iteration caused a retry: for item in some_iterable:
try:
for attempt in Retrying(
label=f"doing stuff with {item}",
stop=stop_after_attempt(5),
before_sleep=before_sleep_log(logger, logging.DEBUG),
wait=wait_fixed(10)):
with attempt:
data = do_stuff(item)
except RetryError:
continue
... |
This code snippet makes me think this should just be an argument to |
So that the snippet would become like this? for item in some_iterable:
try:
for attempt in Retrying(
stop=stop_after_attempt(5),
before_sleep=before_sleep_log(logger, logging.DEBUG, label=f"doing stuff with {item}"),
wait=wait_fixed(10)):
with attempt:
data = do_stuff(item)
except RetryError:
continue
... However fn_name is also used in before_log and after_log. Having a single label shared between them at BaseRetryObject makes a bit more sense than having a separate label for each function. |
If label is set it will be used instead of a function name in before_sleep_log
I've moved the "get label" code to the RetryCallState class and modified all the log functions to use it. |
If label is set it will be used instead of a function name in before_sleep_log.
I didn't find any appropriate place to add a test, so I simply wrote a small script to check that it all works: