Call Django celery on behalf of

I need to call the celery task (in tasks.py) with models.py, the only problem is that tasks.py imports models.py, so I cannot import tasks.py from models.py.

Is there a way to invoke the celery task by simply using its name, without having to import it? A similar thing is implemented for ForeignKey fields for the same reason (preventing cyclic import).

+6
source share
2 answers

Yes there is.

You can use:

from celery.execute import send_task send_task('my_task', [], kwargs) 

Make sure the task function has a name:

 from celery import task @task(name='my_task') def my_task(): ... 

Hope this helps!

+13
source

In celery 3+:

 from celery import Celery app = Celery() app.send_task('my_task', [], kwargs) 
+2
source

Source: https://habr.com/ru/post/955462/


All Articles