Browse Source

Cancelable tasks: Clean-up style

Ask Solem 15 years ago
parent
commit
c96fe89da1
2 changed files with 25 additions and 17 deletions
  1. 18 13
      celery/contrib/cancelable.py
  2. 7 4
      celery/tests/test_task_cancelable.py

+ 18 - 13
celery/contrib/cancelable.py

@@ -28,9 +28,11 @@ In the consumer:
 .. code-block:: python
 .. code-block:: python
 
 
    from celery.contrib.cancelable import CancelableTask
    from celery.contrib.cancelable import CancelableTask
+
    def MyLongRunningTask(CancelableTask):
    def MyLongRunningTask(CancelableTask):
+
        def run(self, **kwargs):
        def run(self, **kwargs):
-           logger = self.get_logger()
+           logger = self.get_logger(**kwargs)
            results = []
            results = []
            for x in xrange(100):
            for x in xrange(100):
                # Check after every 5 loops..
                # Check after every 5 loops..
@@ -38,11 +40,11 @@ In the consumer:
                    if self.is_cancelled(**kwargs):
                    if self.is_cancelled(**kwargs):
                        # Respect the cancelled status and terminate
                        # Respect the cancelled status and terminate
                        # gracefully
                        # gracefully
-                       logger.warning('Task cancelled.')
+                       logger.warning("Task cancelled.")
                        return None
                        return None
                y = do_something_expensive(x)
                y = do_something_expensive(x)
                results.append(y)
                results.append(y)
-           logger.info('Task finished.')
+           logger.info("Task finished.")
            return results
            return results
 
 
 
 
@@ -51,7 +53,9 @@ In the producer:
 .. code-block:: python
 .. code-block:: python
 
 
    from myproject.tasks import MyLongRunningTask
    from myproject.tasks import MyLongRunningTask
+
    def myview(request):
    def myview(request):
+
        async_result = MyLongRunningTask.delay()
        async_result = MyLongRunningTask.delay()
        # async_result is of type CancelableAsyncResult
        # async_result is of type CancelableAsyncResult
 
 
@@ -61,16 +65,15 @@ In the producer:
 
 
        ...
        ...
 
 
-After the `async_result.cancel()` call, the task execution is not
+After the ``async_result.cancel()`` call, the task execution is not
 aborted immediately. In fact, it is not guaranteed to abort at all. Keep
 aborted immediately. In fact, it is not guaranteed to abort at all. Keep
-checking the `async_result` status, or call `async_result.wait()` to
+checking the ``async_result`` status, or call ``async_result.wait()`` to
 have it block until the task is finished.
 have it block until the task is finished.
 
 
 """
 """
-
 from celery.task.base import Task
 from celery.task.base import Task
 from celery.result import AsyncResult
 from celery.result import AsyncResult
-from multiprocessing import Process, Event
+
 
 
 """ Task States
 """ Task States
 
 
@@ -82,11 +85,12 @@ from multiprocessing import Process, Event
 """
 """
 CANCELLED = "CANCELLED"
 CANCELLED = "CANCELLED"
 
 
+
 class CancelableAsyncResult(AsyncResult):
 class CancelableAsyncResult(AsyncResult):
     """Represents a cancelable result.
     """Represents a cancelable result.
 
 
-    Specifically, this gives the AsyncResult a cancel() method, which sets
-    the state of the underlying Task to "CANCELLED".
+    Specifically, this gives the ``AsyncResult`` a :meth:`cancel()` method,
+    which sets the state of the underlying Task to ``"CANCELLED"``.
 
 
     """
     """
 
 
@@ -105,8 +109,10 @@ class CancelableAsyncResult(AsyncResult):
         all).
         all).
 
 
         """
         """
-        # TODO: store_result requires all four arguments to be set, but only status should be updated here
-        return self.backend.store_result(self.task_id, result=None, status=CANCELLED, traceback=None)
+        # TODO: store_result requires all four arguments to be set,
+        # but only status should be updated here
+        return self.backend.store_result(self.task_id, result=None,
+                                         status=CANCELLED, traceback=None)
 
 
 
 
 class CancelableTask(Task):
 class CancelableTask(Task):
@@ -137,8 +143,7 @@ class CancelableTask(Task):
         often (for performance).
         often (for performance).
 
 
         """
         """
-        result = self.AsyncResult(kwargs['task_id'])
+        result = self.AsyncResult(kwargs["task_id"])
         if not isinstance(result, CancelableAsyncResult):
         if not isinstance(result, CancelableAsyncResult):
             return False
             return False
         return result.is_cancelled()
         return result.is_cancelled()
-

+ 7 - 4
celery/tests/test_task_cancelable.py

@@ -1,18 +1,22 @@
 import unittest2 as unittest
 import unittest2 as unittest
 
 
-from celery.contrib.cancelable import CancelableTask, CancelableAsyncResult, CANCELLED
+from celery.contrib.cancelable import CANCELLED
+from celery.contrib.cancelable import CancelableTask, CancelableAsyncResult
+
 
 
 class MyCancelableTask(CancelableTask):
 class MyCancelableTask(CancelableTask):
+
     def run(self, **kwargs):
     def run(self, **kwargs):
         return True
         return True
 
 
+
 class TestCancelableTask(unittest.TestCase):
 class TestCancelableTask(unittest.TestCase):
+
     def test_async_result_is_cancelable(self):
     def test_async_result_is_cancelable(self):
         t = MyCancelableTask()
         t = MyCancelableTask()
         result = t.apply_async()
         result = t.apply_async()
         tid = result.task_id
         tid = result.task_id
-        self.assertIsInstance(t.AsyncResult(tid), \
-                              CancelableAsyncResult)
+        self.assertIsInstance(t.AsyncResult(tid), CancelableAsyncResult)
 
 
     def test_is_not_cancelled(self):
     def test_is_not_cancelled(self):
         t = MyCancelableTask()
         t = MyCancelableTask()
@@ -26,4 +30,3 @@ class TestCancelableTask(unittest.TestCase):
         result.cancel()
         result.cancel()
         tid = result.task_id
         tid = result.task_id
         self.assertTrue(t.is_cancelled(task_id=tid))
         self.assertTrue(t.is_cancelled(task_id=tid))
-