-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "[IMPR] Add a WaitingMixin to Requests and SparqlQuery class"
- Loading branch information
Showing
3 changed files
with
69 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,56 @@ | ||
"""Module providing several layers of data access to the wiki.""" | ||
# | ||
# (C) Pywikibot team, 2007-2022 | ||
# (C) Pywikibot team, 2007-2023 | ||
# | ||
# Distributed under the terms of the MIT license. | ||
# | ||
from typing import Optional | ||
|
||
import pywikibot | ||
|
||
|
||
class WaitingMixin: | ||
|
||
"""A mixin to implement wait cycles. | ||
.. versionadded:: 8.4 | ||
:ivar int max_retries: Maximum number of times to retry an API | ||
request before quitting. Defaults to ``config.max_retries`` if | ||
attribute is missing. | ||
:ivar int retry_wait: Minimum time to wait before resubmitting a | ||
failed API request. Defaults to ``config.retry_wait`` if | ||
attribute is missing. | ||
:ivar int current_retries: counter of retries made for the current | ||
request. Starting with 1 if attribute is missing. | ||
""" | ||
|
||
def wait(self, delay: Optional[int] = None) -> None: | ||
"""Determine how long to wait after a failed request. | ||
:param delay: Minimum time in seconds to wait. Overwrites | ||
``retry_wait`` variable if given. The delay doubles each | ||
retry until ``retry_max`` seconds is reached. | ||
""" | ||
if not hasattr(self, 'max_retries'): | ||
self.max_retries = pywikibot.config.max_retries | ||
|
||
if not hasattr(self, 'retry_wait'): | ||
self.retry_wait = pywikibot.config.retry_wait | ||
|
||
if self.current_retries > self.max_retries: | ||
raise pywikibot.exceptions.TimeoutError( | ||
'Maximum retries attempted without success.') | ||
|
||
if not hasattr(self, 'current_retries'): | ||
self.current_retries = 1 | ||
else: | ||
self.current_retries += 1 | ||
|
||
# double the next wait, but do not exceed config.retry_max seconds | ||
delay = delay or self.retry_wait | ||
delay *= 2 ** (self.current_retries - 1) | ||
delay = min(delay, pywikibot.config.retry_max) | ||
|
||
pywikibot.warning(f'Waiting {delay:.1f} seconds before retrying.') | ||
pywikibot.sleep(delay) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters