Selaa lähdekoodia

Fixes DatabaseTask example. Closes #902

Ask Solem 12 vuotta sitten
vanhempi
commit
7d5ffec723
1 muutettua tiedostoa jossa 22 lisäystä ja 2 poistoa
  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
 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):
+        abstract = True
         _db = None
 
         @property
         def db(self):
-            if self._db = None:
+            if self._db is None:
                 self._db = Database.connect()
             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
 ----------------