Pārlūkot izejas kodu

Adds App.signature

Ask Solem 11 gadi atpakaļ
vecāks
revīzija
34d884d1b5
2 mainītis faili ar 45 papildinājumiem un 18 dzēšanām
  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.force_close_all()
                 amqp._producer_pool = None
                 amqp._producer_pool = None
 
 
+    def signature(self, *args, **kwargs):
+        kwargs['app'] = self
+        return self.canvas.signature(*args, **kwargs)
+
     def create_task_cls(self):
     def create_task_cls(self):
         """Creates a base task class using default configuration
         """Creates a base task class using default configuration
         taken from this app."""
         taken from this app."""
@@ -594,6 +598,11 @@ class Celery(object):
     def log(self):
     def log(self):
         return instantiate(self.log_cls, app=self)
         return instantiate(self.log_cls, app=self)
 
 
+    @cached_property
+    def canvas(self):
+        from celery import canvas
+        return canvas
+
     @cached_property
     @cached_property
     def tasks(self):
     def tasks(self):
         self.finalize()
         self.finalize()

+ 36 - 18
docs/reference/celery.rst

@@ -19,7 +19,7 @@ and creating Celery applications.
 :class:`group`        group tasks together
 :class:`group`        group tasks together
 :class:`chain`        chain tasks together
 :class:`chain`        chain tasks together
 :class:`chord`        chords enable callbacks for groups
 :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_app`   proxy to the current application instance
 :data:`current_task`  proxy to the currently executing task
 :data:`current_task`  proxy to the currently executing task
 ===================== ===================================================
 ===================== ===================================================
@@ -131,6 +131,11 @@ and creating Celery applications.
                 with app.connection() as conn:
                 with app.connection() as conn:
                     pass
                     pass
 
 
+    .. method:: Celery.signature
+
+        Return a new :class:`~celery.canvas.Signature` bound to this app.
+        See :meth:`~celery.signature`
+
     .. method:: Celery.bugreport
     .. method:: Celery.bugreport
 
 
         Return a string with information useful for the Celery core
         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
     The body is applied with the return values of all the header
     tasks as a list.
     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.
     Describes the arguments and execution options for a single task invocation.
 
 
     Used as the parts in a :class:`group` or to safely pass
     Used as the parts in a :class:`group` or to safely pass
     tasks around as callbacks.
     tasks around as callbacks.
 
 
-    Subtasks can also be created from tasks::
+    Signatures can also be created from tasks::
 
 
         >>> add.subtask(args=(), kwargs={}, options={})
         >>> 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
     arguments will be ignored and the values in the dict will be used
     instead.
     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={}}
         {"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`.
         Shortcut to :meth:`apply_async`.
 
 
-    .. method:: subtask.apply_async(args=(), kwargs={}, ...)
+    .. method:: signature.apply_async(args=(), kwargs={}, ...)
 
 
         Apply this task asynchronously.
         Apply this task asynchronously.
 
 
@@ -467,46 +476,55 @@ See :ref:`guide-canvas` for more about creating task workflows.
 
 
         See :meth:`~@Task.apply_async`.
         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
         Same as :meth:`apply_async` but executed the task inline instead
         of sending a task message.
         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 args: Partial args to be prepended to the existing args.
         :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
         :keyword kwargs: Partial kwargs to be merged with the existing kwargs.
         :keyword options: Partial options to be merged with the existing
         :keyword options: Partial options to be merged with the existing
                           options.
                           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`.
         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
         Add a callback task to be applied if this task
         executes successfully.
         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
         Add a callback task to be applied if an error occurs
         while executing this task.
         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(...)``).
         Set arbitrary options (same as ``.options.update(...)``).
 
 
         This is a chaining method call (i.e. it will return ``self``).
         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,
         Gives a recursive list of dependencies (unchain if you will,
         but with links intact).
         but with links intact).