Add test.support.busy_retry()#93770
Conversation
"Configure ccache action" failed with: "Error: reserveCache failed: Cache service responded with 503" |
|
|
When writing this PR, I saw code that I wanted to change, but I tried to minimize changes of this PR :-D I will write a follow-up PR to enhance tests. |
|
Nice. But some new code is not equivalent to the old code (in particularly related to signal handling). In some cases the old code used very short sleep or no sleep, or other operation instead of sleep. I am not sure that replacements preserve the intention of tests. |
Oh sorry, I reverted these changes. Are remaining changes correct? |
|
I dislike |
|
The "busy wait" term seems commonly used: https://en.wikipedia.org/wiki/Busy_waiting |
You can merge both functions if skip I though also about alternative interface. Instead of you could write write (looks like (it is closer to the original code). But I do not want to start bikeshedding. It is all wild fantasy. These functions are for internal use. The current code of the PR is good enough. |
|
bikeshedding: busy_retry and sleeping_retry. |
First, I wanted to do something like that, but I dislike defining nested functions or lambda functions. If you read the PR, you can see that existing tests look the same with my change. There is no indirection (function call), the code remains "flat". |
|
increasing_sleep() is a bad name. The delay is not "increasing", it's exponential. What the function does is more an: https://en.wikipedia.org/wiki/Exponential_backoff IMO tenacity API https://tenacity.readthedocs.io/en/latest/api.html#wait-functions should be a good inspiration for function names. I renamed increasing_sleep() to wait_exponential(). |
Yes, I would expect the word "repeat" in the name too. But I do not want to make it a requirement.
I understood you. While in some cases it is easy to write a lambda, in other cases, when you use several statement in the loop or even try/except block, the local function is unavoidable, and it makes the code less straight.
I think that the name should describe the purpose of the function, not the implementation detail. In some cases linear grow of delays (and quadratic of total time) may be more preferable than exponential grow. What do you think about merging two functions into a single function? |
|
IMO the two functions are too different and deserve to have different names. In terms of API, currently, I propose:
@methane proposed:
tenacity API seems to be:
I like to put "sleep" in the name, so it's easy to understand that the function calls time.sleep(). I have no preference between "retry and "wait". Maybe retry better highlights that it's a loop and it calls the loop body multiple times until it "succeed" (somehow). In short, I like names proposed by @methane. @serhiy-storchaka: do you have a preference? By the way, the whole API is: Are you fine with init_delay and max_delay parameter names? If not, do you have a suggestion? Well, I'm not sure that the API matters so much, it's a test module :-D It must not be used outside Python ;-) |
|
I like @methane proposal. |
Add busy_retry() and sleeping_retry() functions to test.support.
|
Thanks @serhiy-storchaka and @methane for your reviews :-) I merged my PR. IMO the tests are now easier to read with the changes. Also, they might be faster (if the condition becomes true quickly) and uses less CPU (lower system load) if the condition takes seconds to complete (sleep longer). |
|
GH-110341 is a backport of this pull request to the 3.11 branch. |
Add busy_wait() and increasing_sleep() functions to test.support.