I am using celery with my django project.
In the celery tasks file, I need to import my models, in order to trigger model methods. However, I would also like my model to be able to trigger certain celery tasks.
Right now I am importing my models to celery, however trying to import celery tasks into my models file results in an import loop and import error.
What is the correct way to go about this?
The "shared_task" decorator allows creation of Celery tasks for reusable apps as it doesn't need the instance of the Celery app. It is also easier way to define a task as you don't need to import the Celery app instance.
Here are some key points: DJANGO_SETTINGS_MODULE must be set in the environment before starting a Celery process. Its presence in the environment triggers internal magic in Celery to run the Django setup at the right time. The Celery "application" must be created and configured during startup of both Django and Celery.
Celery provides the send_task()
method, which allows to send a task by name, thus eliminating the need to import it - for example:
# models.py
from celery import current_app
# no need to import do_stuff from tasks because it will be sent by name
current_app.send_task('myapp.tasks.do_stuff', args=(1, 'two'), kwargs={'foo': 'bar'})
More in the documentation.
What I ended up doing, is using imports within methods, instead of a general import at the top of the models file. Obviously, i didn't really need circular imports. My problem was that I was importing the model at the top of the celery tasks file and importing the celery tasks at the top of the models file. That wasn't really necessary. by compartmentalizing the imports I was able to avoid the circular import problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With