Jelajahi Sumber

TaskSet: Now shows DeprecationWarning when using old-style single-task syntax.

Ask Solem 15 tahun lalu
induk
melakukan
9040ec3e5b
2 mengubah file dengan 65 tambahan dan 21 penghapusan
  1. 53 8
      celery/task/sets.py
  2. 12 13
      celery/tests/test_task.py

+ 53 - 8
celery/task/sets.py

@@ -1,3 +1,5 @@
+import warnings
+
 from UserList import UserList
 from UserList import UserList
 
 
 from celery import conf
 from celery import conf
@@ -8,6 +10,28 @@ from celery.messaging import TaskPublisher
 from celery.result import TaskSetResult
 from celery.result import TaskSetResult
 from celery.utils import gen_unique_id
 from celery.utils import gen_unique_id
 
 
+TASKSET_DEPRECATION_TEXT = """\
+Using this invocation of TaskSet is deprecated and will be removed
+in Celery v2.4!
+
+TaskSets now supports multiple types of tasks, the API has to reflect
+this so the syntax has been changed to:
+
+    from celery.task.sets import TaskSet
+
+    ts = TaskSet(tasks=[
+            %(cls)s.subtask(args1, kwargs1, options1),
+            %(cls)s.subtask(args2, kwargs2, options2),
+            %(cls)s.subtask(args3, kwargs3, options3),
+            ...
+            %(cls)s.subtask(argsN, kwargsN, optionsN),
+    ])
+
+    result = ts.apply_async()
+
+Thank you for your patience!
+
+"""
 
 
 class subtask(AttributeDict):
 class subtask(AttributeDict):
     """Class that wraps the arguments and execution options
     """Class that wraps the arguments and execution options
@@ -93,17 +117,23 @@ class TaskSet(UserList):
         >>> list_of_return_values = taskset_result.join()
         >>> list_of_return_values = taskset_result.join()
 
 
     """
     """
-    task = None # compat
-    task_name = None # compat
+    _task = None # compat
+    _task_name = None # compat
 
 
     def __init__(self, task=None, tasks=None):
     def __init__(self, task=None, tasks=None):
-        # Previously TaskSet only supported applying one kind of task.
-        # the signature then was TaskSet(task, arglist)
-        # Convert the arguments to subtasks'.
         if task is not None:
         if task is not None:
-            tasks = [subtask(task, *arglist) for arglist in tasks]
-            self.task = task
-            self.task_name = task.name
+            if hasattr(task, "__iter__"):
+                tasks = task
+            else:
+                # Previously TaskSet only supported applying one kind of task.
+                # the signature then was TaskSet(task, arglist),
+                # so convert the arguments to subtasks'.
+                tasks = [subtask(task, *arglist) for arglist in tasks]
+                task = self._task = registry.tasks[task.name]
+                self._task_name = task.name
+                warnings.warn(TASKSET_DEPRECATION_TEXT % {
+                                "cls": task.__class__.__name__},
+                              DeprecationWarning)
 
 
         self.data = list(tasks)
         self.data = list(tasks)
         self.total = len(self.tasks)
         self.total = len(self.tasks)
@@ -165,3 +195,18 @@ class TaskSet(UserList):
     @property
     @property
     def tasks(self):
     def tasks(self):
         return self.data
         return self.data
+
+    @property
+    def task(self):
+        warnings.warn(
+            "TaskSet.task is deprecated and will be removed in 1.4",
+            DeprecationWarning)
+        return self._task
+
+    @property
+    def task_name(self):
+        warnings.warn(
+            "TaskSet.task_name is deprecated and will be removed in 1.4",
+            DeprecationWarning)
+        return self._task_name
+

+ 12 - 13
celery/tests/test_task.py

@@ -352,25 +352,24 @@ class TestTaskSet(unittest.TestCase):
 
 
     @with_eager_tasks
     @with_eager_tasks
     def test_function_taskset(self):
     def test_function_taskset(self):
-        ts = task.TaskSet(return_True_task.name, [
-              ([1], {}), [[2], {}], [[3], {}], [[4], {}], [[5], {}]])
+        subtasks = [return_True_task.subtask([i]) for i in range(1, 6)]
+        ts = task.TaskSet(subtasks)
         res = ts.apply_async()
         res = ts.apply_async()
         self.assertListEqual(res.join(), [True, True, True, True, True])
         self.assertListEqual(res.join(), [True, True, True, True, True])
 
 
     def test_counter_taskset(self):
     def test_counter_taskset(self):
         IncrementCounterTask.count = 0
         IncrementCounterTask.count = 0
-        ts = task.TaskSet(IncrementCounterTask, [
-            ([], {}),
-            ([], {"increment_by": 2}),
-            ([], {"increment_by": 3}),
-            ([], {"increment_by": 4}),
-            ([], {"increment_by": 5}),
-            ([], {"increment_by": 6}),
-            ([], {"increment_by": 7}),
-            ([], {"increment_by": 8}),
-            ([], {"increment_by": 9}),
+        ts = task.TaskSet(tasks=[
+            IncrementCounterTask.subtask((), {}),
+            IncrementCounterTask.subtask((), {"increment_by": 2}),
+            IncrementCounterTask.subtask((), {"increment_by": 3}),
+            IncrementCounterTask.subtask((), {"increment_by": 4}),
+            IncrementCounterTask.subtask((), {"increment_by": 5}),
+            IncrementCounterTask.subtask((), {"increment_by": 6}),
+            IncrementCounterTask.subtask((), {"increment_by": 7}),
+            IncrementCounterTask.subtask((), {"increment_by": 8}),
+            IncrementCounterTask.subtask((), {"increment_by": 9}),
         ])
         ])
-        self.assertEqual(ts.task_name, IncrementCounterTask.name)
         self.assertEqual(ts.total, 9)
         self.assertEqual(ts.total, 9)