浏览代码

Added FAQ: How can I run a task once another task has finished?

Ask Solem 15 年之前
父节点
当前提交
7c2ecfd293
共有 1 个文件被更改,包括 26 次插入2 次删除
  1. 26 2
      FAQ

+ 26 - 2
FAQ

@@ -322,7 +322,7 @@ as a message. If you don't collect these results, they will build up and
 RabbitMQ will eventually run out of memory.
 RabbitMQ will eventually run out of memory.
 
 
 If you don't use the results for a task, make sure you set the
 If you don't use the results for a task, make sure you set the
-``ignore_result`` option::
+``ignore_result`` option:
 
 
 .. code-block python
 .. code-block python
 
 
@@ -420,6 +420,30 @@ using the STOMP backend:
 Features
 Features
 ========
 ========
 
 
+How can I run a task once another task has finished?
+----------------------------------------------------
+
+You can safely launch a task inside a task.
+Also, a common pattern is to use callback tasks:
+
+.. code-block:: python
+
+    @task()
+    def add(x, y, callback=None):
+        result = x + y
+        if callback:
+            callback.delay(result)
+        return result
+
+
+    @task(ignore_result=True)
+    def log_result(result, **kwargs):
+        logger = log_result.get_logger(**kwargs)
+        logger.info("log_result got: %s" % (result, ))
+
+
+    >>> add.delay(2, 2, callback=log_result)
+
 Can I send some tasks to only some servers?
 Can I send some tasks to only some servers?
 --------------------------------------------
 --------------------------------------------
 
 
@@ -733,7 +757,7 @@ done.
             translation.activate(prev_language)
             translation.activate(prev_language)
 
 
 The common pattern here would be for the task to take a ``language``
 The common pattern here would be for the task to take a ``language``
-argument::
+argument:
 
 
 .. code-block:: python
 .. code-block:: python