jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/370369 )

Change subject: [IMPR] Use context manager to acquire/release locks
......................................................................


[IMPR] Use context manager to acquire/release locks

Change-Id: I7e94fd8d86e4dec125cbaa7c625498ec892476e9
---
M pywikibot/throttle.py
1 file changed, 5 insertions(+), 18 deletions(-)

Approvals:
  Mpaa: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index 24d2266..71c0da8 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 """Mechanics to slow down wiki read and/or write rate."""
 #
-# (C) Pywikibot team, 2008
+# (C) Pywikibot team, 2008-2017
 #
 # Distributed under the terms of the MIT license.
 #
@@ -78,11 +78,10 @@
     def checkMultiplicity(self):
         """Count running processes for site and set process_multiplicity."""
         global pid
-        self.lock.acquire()
         mysite = self.mysite
         pywikibot.debug(u"Checking multiplicity: pid = %(pid)s" % globals(),
                         _logger)
-        try:
+        with self.lock:
             processes = []
             my_pid = pid or 1  # start at 1 if global pid not yet set
             count = 1
@@ -139,13 +138,10 @@
             pywikibot.log(
                 'Found {0} {1} processes running, including this one.'.format(
                     count, mysite))
-        finally:
-            self.lock.release()
 
     def setDelays(self, delay=None, writedelay=None, absolute=False):
         """Set the nominal delays in seconds. Defaults to config values."""
-        self.lock.acquire()
-        try:
+        with self.lock:
             if delay is None:
                 delay = self.mindelay
             if writedelay is None:
@@ -158,8 +154,6 @@
                                   self.maxdelay)
             # Start the delay count now, not at the next check
             self.last_read = self.last_write = time.time()
-        finally:
-            self.lock.release()
 
     def getDelay(self, write=False):
         """Return the actual delay, accounting for multiple processes.
@@ -263,8 +257,7 @@
         thread from writing to the same site until the wait expires.
 
         """
-        self.lock.acquire()
-        try:
+        with self.lock:
             wait = self.waittime(write=write)
             # Calculate the multiplicity of the next delay based on how
             # big the request is that is being posted now.
@@ -279,8 +272,6 @@
                 self.last_write = time.time()
             else:
                 self.last_read = time.time()
-        finally:
-            self.lock.release()
 
     def lag(self, lagtime):
         """Seize the throttle lock due to server lag.
@@ -289,8 +280,7 @@
 
         """
         started = time.time()
-        self.lock.acquire()
-        try:
+        with self.lock:
             # start at 1/2 the current server lag time
             # wait at least 5 seconds but not more than 120 seconds
             delay = min(max(5, lagtime // 2), 120)
@@ -298,6 +288,3 @@
             wait = delay - (time.time() - started)
 
             self.wait(wait)
-
-        finally:
-            self.lock.release()

-- 
To view, visit https://gerrit.wikimedia.org/r/370369
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7e94fd8d86e4dec125cbaa7c625498ec892476e9
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Magul <tomasz.magul...@gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.w...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to