소스 검색

Fixes DatabaseTask example. Closes #902

Ask Solem 12 년 전
부모
커밋
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
 ----------------
 ----------------