As Celery documentation tells, assume there is a custom class called DatabaseTask.
from celery import Task
class DatabaseTask(Task):
_db = None
@property
def db(self):
if self._db is None:
self._db = Database.connect()
return self._db
This custom class can be passed in Celery app when instantiating the app.
app = Celery('tasks', DatabaseTask)
Now, how to use db property in tasks that are declared using the decorator syntax within your app. For example, How can I use db property in collect_data task?
@app.task
def collect_data():
#
#