jenkins-bot has submitted this change and it was merged.
Change subject: Refactor common waiting code in throttle.py
......................................................................
Refactor common waiting code in throttle.py
Both lag() and __call__() shared the exact same code to wait for some
seconds and eventually announce the delay. Refactor the code to a new
method throttle.wait(seconds).
Change-Id: Id1d34357ec04caac17bea120c0d079ce8a5a3dbe
---
M pywikibot/throttle.py
1 file changed, 26 insertions(+), 33 deletions(-)
Approvals:
John Vandenberg: Looks good to me, but someone else must approve
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index df1fd71..ffd4367 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -229,6 +229,27 @@
return
f.close()
+ def wait(self, seconds):
+ """Wait for seconds seconds.
+
+ Announce the delay if it exceeds a preset limit.
+
+ """
+ if seconds <= 0:
+ return
+
+ message = (u"Sleeping for %(seconds).1f seconds, %(now)s" % {
+ 'seconds': seconds,
+ 'now': time.strftime("%Y-%m-%d %H:%M:%S",
+ time.localtime())
+ })
+ if seconds > config.noisysleep:
+ pywikibot.output(message)
+ else:
+ pywikibot.log(message)
+
+ time.sleep(seconds)
+
def __call__(self, requestsize=1, write=False):
"""Block the calling program if the throttle time has not
expired.
@@ -248,24 +269,9 @@
# size of the request. Getting 64 pages at once allows 6 times
# the delay time for the server.
self.next_multiplicity = math.log(1 + requestsize) / math.log(2.0)
- # Announce the delay if it exceeds a preset limit
- if wait > 0:
- if wait > config.noisysleep:
- pywikibot.output(
- u"Sleeping for %(wait).1f seconds, %(now)s"
- % {'wait': wait,
- 'now': time.strftime("%Y-%m-%d %H:%M:%S",
- time.localtime())
- })
- else:
- pywikibot.log(
- u"Sleeping for %(wait).1f seconds, %(now)s"
- % {'wait': wait,
- 'now': time.strftime("%Y-%m-%d %H:%M:%S",
- time.localtime())
- })
- time.sleep(wait)
+ self.wait(wait)
+
if write:
self.last_write = time.time()
else:
@@ -287,21 +293,8 @@
delay = min(max(5, lagtime // 2), 120)
# account for any time we waited while acquiring the lock
wait = delay - (time.time() - started)
- if wait > 0:
- if wait > config.noisysleep:
- pywikibot.output(
- u"Sleeping for %(wait).1f seconds, %(now)s"
- % {'wait': wait,
- 'now': time.strftime("%Y-%m-%d %H:%M:%S",
- time.localtime())
- })
- else:
- pywikibot.log(
- u"Sleeping for %(wait).1f seconds, %(now)s"
- % {'wait': wait,
- 'now': time.strftime("%Y-%m-%d %H:%M:%S",
- time.localtime())
- })
- time.sleep(wait)
+
+ self.wait(wait)
+
finally:
self.lock.release()
--
To view, visit
https://gerrit.wikimedia.org/r/144323
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id1d34357ec04caac17bea120c0d079ce8a5a3dbe
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Hashar <hashar(a)free.fr>
Gerrit-Reviewer: Hashar <hashar(a)free.fr>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipedia(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot <>