Commits

Valentino Volonghi aka dialtone  committed 860d426

fix a small bug and release a new version

  • Participants
  • Parent commits f516a09

Comments (0)

Files changed (3)

File examples/basic_client.py

 
     url = sys.argv[1]
 
-    if priority:
-        request_asap(url)
-    else:
-        request(url)
+    try:
+        if priority:
+            request_asap(url)
+        else:
+            request(url)
+    except urllib2.URLError, e:
+        print e, dir(e), e.args[0].errno

File turtl/__init__.py

-__version__ = '0.0.3-dev'
+__version__ = '0.0.4'

File turtl/engine.py

 
 class ThrottlingDeferred(_ConcurrencyPrimitive):
     def __init__(self, concurrency, calls, interval):
-        """
-        Throttling deferred that considers both the concurrency
+        """Throttling deferred that considers both the concurrency
         requirements and the frequency, over time, of calls that
         you are allowed to make. It's clear however that if the
         rate of calls is higher than the tokens there will be
         self.calls = calls
         self.interval = interval
         self.points = calls
+        self.stopping = False
 
         self._resetLoop = task.LoopingCall(self._reset)
         self._resetLoop.start(interval, now=False)
 
     def _reset(self):
+        # We don't want to release while calls are in timeout mode
+        # so we schedule the release to run right after a request
+        # has completed.
         self.points = self.calls
-        self.release()
+        self._sem.run(self.release)
 
     def acquire(self, priority=False):
         """Attempt to acquire the token.
         Should be called by whoever did the acquire() when the shared
         resource is free.
         """
-        if self.points > 0 and self.waiting:
+        if not self.stopping and self.points > 0 and self.waiting:
             self.points = self.points - 1
             d = self.waiting.pop(0)
             d.callback(self)
 
+    def stop(self):
+        """Stop the reset calls and cleanup all the unfired deferreds.
+        """
+        self.stopping = True
+    
+        if self._resetLoop.running:
+            self._resetLoop.stop()
+
+        for d in self.waiting:
+            d.errback(defer.CancelledError("ThrottlingDeferred.stop was called"))
+