Przeglądaj źródła

Adds App.signature

Ask Solem 11 lat temu
rodzic
commit
34d884d1b5
2 zmienionych plików z 45 dodań i 18 usunięć
  1. 9 0
      celery/app/base.py
  2. 36 18
      docs/reference/celery.rst

+ 9 - 0
celery/app/base.py

@@ -427,6 +427,10 @@ class Celery(object):
                 amqp._producer_pool.force_close_all()
                 amqp._producer_pool = None
 
+    def signature(self, *args, **kwargs):
+        kwargs['app'] = self
+        return self.canvas.signature(*args, **kwargs)
+
     def create_task_cls(self):
         """Creates a base task class using default configuration
         taken from this app."""
@@ -594,6 +598,11 @@ class Celery(object):
     def log(self):
         return instantiate(self.log_cls, app=self)
 
+    @cached_property
+    def canvas(self):
+        from celery import canvas
+        return canvas
+
     @cached_property
     def tasks(self):
         self.finalize()

+ 36 - 18
docs/reference/celery.rst

@@ -19,7 +19,7 @@ and creating Celery applications.
 :class:`group`        group tasks together
 :class:`chain`        chain tasks together
 :class:`chord`        chords enable callbacks for groups
-:class:`subtask`      task signatures
+:class:`signature`    object describing a task invocation
 :data:`current_app`   proxy to the current application instance
 :data:`current_task`  proxy to the currently executing task
 ===================== ===================================================
@@ -131,6 +131,11 @@ and creating Celery applications.
                 with app.connection() as conn:
                     pass
 
+    .. method:: Celery.signature
+
+        Return a new :class:`~celery.canvas.Signature` bound to this app.
+        See :meth:`~celery.signature`
+
     .. method:: Celery.bugreport
 
         Return a string with information useful for the Celery core
@@ -424,14 +429,14 @@ See :ref:`guide-canvas` for more about creating task workflows.
     The body is applied with the return values of all the header
     tasks as a list.
 
-.. class:: subtask(task=None, args=(), kwargs={}, options={})
+.. class:: signature(task=None, args=(), kwargs={}, options={})
 
     Describes the arguments and execution options for a single task invocation.
 
     Used as the parts in a :class:`group` or to safely pass
     tasks around as callbacks.
 
-    Subtasks can also be created from tasks::
+    Signatures can also be created from tasks::
 
         >>> add.subtask(args=(), kwargs={}, options={})
 
@@ -448,15 +453,19 @@ See :ref:`guide-canvas` for more about creating task workflows.
     arguments will be ignored and the values in the dict will be used
     instead.
 
-        >>> s = subtask("tasks.add", args=(2, 2))
-        >>> subtask(s)
+        >>> s = signature("tasks.add", args=(2, 2))
+        >>> signature(s)
         {"task": "tasks.add", args=(2, 2), kwargs={}, options={}}
 
-    .. method:: subtask.delay(*args, \*\*kwargs)
+    .. method:: signature.__call__(*args \*\*kwargs)
+
+        Call the task directly (in the current process).
+
+    .. method:: signature.delay(*args, \*\*kwargs)
 
         Shortcut to :meth:`apply_async`.
 
-    .. method:: subtask.apply_async(args=(), kwargs={}, ...)
+    .. method:: signature.apply_async(args=(), kwargs={}, ...)
 
         Apply this task asynchronously.
 
@@ -467,46 +476,55 @@ See :ref:`guide-canvas` for more about creating task workflows.
 
         See :meth:`~@Task.apply_async`.
 
-    .. method:: subtask.apply(args=(), kwargs={}, ...)
+    .. method:: signature.apply(args=(), kwargs={}, ...)
 
         Same as :meth:`apply_async` but executed the task inline instead
         of sending a task message.
 
-    .. method:: subtask.clone(args=(), kwargs={}, ...)
+    .. method:: signature.freeze(_id=None)
+
+        Finalize the signature by adding a concrete task id.
+        The task will not be called and you should not call the signature
+        twice after freezing it as that will result in two task messages
+        using the same task id.
+
+        :returns: :class:`@AsyncResult` instance.
+
+    .. method:: signature.clone(args=(), kwargs={}, ...)
 
-        Return a copy of this subtask.
+        Return a copy of this signature.
 
         :keyword args: Partial args to be prepended to the existing args.
         :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
         :keyword options: Partial options to be merged with the existing
                           options.
 
-    .. method:: subtask.replace(args=None, kwargs=None, options=None)
+    .. method:: signature.replace(args=None, kwargs=None, options=None)
 
-        Replace the args, kwargs or options set for this subtask.
+        Replace the args, kwargs or options set for this signature.
         These are only replaced if the selected is not :const:`None`.
 
-    .. method:: subtask.link(other_subtask)
+    .. method:: signature.link(other_signature)
 
         Add a callback task to be applied if this task
         executes successfully.
 
-        :returns: ``other_subtask`` (to work with :func:`~functools.reduce`).
+        :returns: ``other_signature`` (to work with :func:`~functools.reduce`).
 
-    .. method:: subtask.link_error(other_subtask)
+    .. method:: signature.link_error(other_signature)
 
         Add a callback task to be applied if an error occurs
         while executing this task.
 
-        :returns: ``other_subtask`` (to work with :func:`~functools.reduce`)
+        :returns: ``other_signature`` (to work with :func:`~functools.reduce`)
 
-    .. method:: subtask.set(...)
+    .. method:: signature.set(...)
 
         Set arbitrary options (same as ``.options.update(...)``).
 
         This is a chaining method call (i.e. it will return ``self``).
 
-    .. method:: subtask.flatten_links()
+    .. method:: signature.flatten_links()
 
         Gives a recursive list of dependencies (unchain if you will,
         but with links intact).