Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django import loop between celery tasks and my models

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?

like image 888
Ilan lewin Avatar asked Jun 26 '13 07:06

Ilan lewin


People also ask

What is Shared_task in Celery?

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.

How do I schedule a task in Django Celery?

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.


2 Answers

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.

like image 79
rafalmp Avatar answered Oct 17 '22 15:10

rafalmp


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

like image 27
Ilan lewin Avatar answered Oct 17 '22 15:10

Ilan lewin