Prechádzať zdrojové kódy

Use subtask.delay instead of subtask.apply_async to do star argument merging.

Ask Solem 14 rokov pred
rodič
commit
68d5234f04
3 zmenil súbory, kde vykonal 24 pridanie a 24 odobranie
  1. 14 8
      celery/task/sets.py
  2. 4 3
      docs/userguide/tasks.rst
  3. 6 13
      docs/userguide/tasksets.rst

+ 14 - 8
celery/task/sets.py

@@ -73,19 +73,25 @@ class subtask(AttributeDict):
         init(task=task_name, args=tuple(args or ()), kwargs=kwargs or (),
              options=options or ())
 
-    def apply(self, *argmerge, **execopts):
+    def delay(self, *argmerge, **kwmerge):
+        """Shortcut to ``apply_async(argmerge, kwargs)``."""
+        return self.apply_async(args=argmerge, kwargs=kwmerge)
+
+    def apply(self, args, kwargs, **options):
         """Apply this task locally."""
         # For callbacks: extra args are prepended to the stored args.
-        args = tuple(argmerge) + tuple(self.args)
-        return self.get_type().apply(args, self.kwargs,
-                                     **dict(self.options, **execopts))
+        args = tuple(args) + tuple(self.args)
+        kwargs = dict(self.kwargs, **kwargs)
+        options = dict(self.options, **options)
+        return self.get_type().apply(args, kwargs, options)
 
-    def apply_async(self, *argmerge, **execopts):
+    def apply_async(self, args, kwargs, **options):
         """Apply this task asynchronously."""
         # For callbacks: extra args are prepended to the stored args.
-        args = tuple(argmerge) + tuple(self.args)
-        return self.get_type().apply_async(args, self.kwargs,
-                                           **dict(self.options, **execopts))
+        args = tuple(args) + tuple(self.args)
+        kwargs = dict(self.kwargs, **kwargs)
+        options = dict(self.options, **options)
+        return self.get_type().apply_async(args, kwargs, options)
 
     def get_type(self):
         # For JSON serialization, the task class is lazily loaded,

+ 4 - 3
docs/userguide/tasks.rst

@@ -543,13 +543,13 @@ Good:
             # The callback may have been serialized with JSON,
             # so best practice is to convert the subtask dict back
             # into a subtask object.
-            subtask(callback).apply_async(page)
+            subtask(callback).delay(page)
 
     @task(ignore_result=True)
     def parse_page(url, page, callback=None):
         info = myparser.parse_document(page)
         if callback:
-            subtask(callback).apply_async(url, info)
+            subtask(callback).delay(url, info)
 
     @task(ignore_result=True)
     def store_page_info(url, info):
@@ -559,7 +559,8 @@ Good:
 We use :class:`~celery.task.sets.subtask` here to safely pass
 around the callback task. :class:`~celery.task.sets.subtask` is a 
 subclass of dict used to wrap the arguments and execution options
-for a single task invocation.
+for a single task invocation. See :doc:`tasksets` for more information about
+subtasks.
 
 
 Performance and Strategies

+ 6 - 13
docs/userguide/tasksets.rst

@@ -42,30 +42,23 @@ takes the result as an argument::
     def add(x, y, callback=None):
         result = x + y
         if callback is not None:
-            subtask(callback).apply_async(result)
+            subtask(callback).delay(result)
         return result
 
 See? :class:`~celery.task.sets.subtask` also knows how it should be applied,
-asynchronously by :meth:`~celery.task.sets.subtask.apply_async`, and
+asynchronously by :meth:`~celery.task.sets.subtask.delay`, and
 eagerly by :meth:`~celery.task.sets.subtask.apply`.
 
-The best thing is that any arguments you add to ``subtask.apply_async``,
+The best thing is that any arguments you add to ``subtask.delay``,
 will be prepended to the arguments specified by the subtask itself!
 
-:note: additional keyword arguments will be added to the
-  execution options, not the task keyword arguments.
-
 So if you have the subtask::
 
-    >>> add.subtask(args=(10, ), options={"ignore_result": True})
-
-``subtask.apply_async(result)`` becomes::
-
-    >>> add.apply_async(args=(result, 10), ignore_result=True)
+    >>> add.subtask(args=(10, ))
 
-and ``subtask.apply_async(result, ignore_result=False)`` becomes::
+``subtask.delay(result)`` becomes::
 
-    >>> add.apply_async(args=(result, 10), ignore_result=False)
+    >>> add.apply_async(args=(result, 10))
 
 Now let's execute our new ``add`` task with a callback::