瀏覽代碼

Fixes DatabaseTask example. Closes #902

Ask Solem 13 年之前
父節點
當前提交
7d5ffec723
共有 1 個文件被更改,包括 22 次插入2 次删除
  1. 22 2
      docs/userguide/tasks.rst

+ 22 - 2
docs/userguide/tasks.rst

@@ -814,17 +814,37 @@ If you have a task,
 And you route every request to the same process, then it
 And you route every request to the same process, then it
 will keep state between requests.
 will keep state between requests.
 
 
-This can also be useful to keep cached resources::
+This can also be useful to cache resources,
+e.g. a base Task class that caches a database connection:
+
+.. code-block:: python
+
+    from celery import Task
 
 
     class DatabaseTask(Task):
     class DatabaseTask(Task):
+        abstract = True
         _db = None
         _db = None
 
 
         @property
         @property
         def db(self):
         def db(self):
-            if self._db = None:
+            if self._db is None:
                 self._db = Database.connect()
                 self._db = Database.connect()
             return self._db
             return self._db
 
 
+
+that can be added to tasks like this:
+
+.. code-block:: python
+
+
+    @celery.task(base=DatabaseTask)
+    def process_rows():
+        for row in self.db.table.all():
+            ...
+
+The ``db`` attribute of the ``process_rows`` task will then
+always stay the same in each process.
+
 Abstract classes
 Abstract classes
 ----------------
 ----------------