Преглед на файлове

Extend Task.rate_limit to accept float values

Celery is enforcing rate_limit integer values, but the kombu TokenBucket implementation
fill_rate value is casted to float anyway, therefore remove this limitation.
Christoph Krybus преди 12 години
родител
ревизия
2469687634
променени са 3 файла, в които са добавени 6 реда и са изтрити 4 реда
  1. 3 1
      celery/tests/slow/test_buckets.py
  2. 2 2
      celery/utils/timeutils.py
  3. 1 1
      docs/userguide/tasks.rst

+ 3 - 1
celery/tests/slow/test_buckets.py

@@ -103,13 +103,15 @@ class test_rate_limit_string(Case):
     @skip_if_disabled
     def test_conversion(self):
         self.assertEqual(timeutils.rate(999), 999)
+        self.assertEqual(timeutils.rate(7.5), 7.5)
+        self.assertEqual(timeutils.rate('2.5/s'), 2.5)
         self.assertEqual(timeutils.rate('1456/s'), 1456)
         self.assertEqual(timeutils.rate('100/m'),
                          100 / 60.0)
         self.assertEqual(timeutils.rate('10/h'),
                          10 / 60.0 / 60.0)
 
-        for zero in (0, None, '0', '0/m', '0/h', '0/s'):
+        for zero in (0, None, '0', '0/m', '0/h', '0/s', '0.0/s'):
             self.assertEqual(timeutils.rate(zero), 0)
 
 

+ 2 - 2
celery/utils/timeutils.py

@@ -218,12 +218,12 @@ def remaining(start, ends_in, now=None, relative=False, debug=False):
 
 
 def rate(rate):
-    """Parses rate strings, such as `"100/m"` or `"2/h"`
+    """Parses rate strings, such as `"100/m"`, `"2/h"` or `"0.5/s"`
     and converts them to seconds."""
     if rate:
         if isinstance(rate, basestring):
             ops, _, modifier = rate.partition('/')
-            return RATE_MODIFIER_MAP[modifier or 's'](int(ops)) or 0
+            return RATE_MODIFIER_MAP[modifier or 's'](float(ops)) or 0
         return rate or 0
     return 0
 

+ 1 - 1
docs/userguide/tasks.rst

@@ -425,7 +425,7 @@ General
     start.
 
     If this is :const:`None` no rate limit is in effect.
-    If it is an integer, it is interpreted as "tasks per second".
+    If it is an integer or float, it is interpreted as "tasks per second".
 
     The rate limits can be specified in seconds, minutes or hours
     by appending `"/s"`, `"/m"` or `"/h"` to the value.