14
votes

I need to call a celery task (in tasks.py) from models.py, the only problem is, tasks.py imports models.py, so I can't import tasks.py from models.py.

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

2
Did you try celery.execute.send_task('mod.task_func', [arg1, arg2], {kwarg: kwvalue})? - falsetru

2 Answers

19
votes

Yes, there is.

You can use:

from celery.execute import send_task    

send_task('my_task', [], kwargs)

Be sure that you task function has a name:

from celery import task

@task(name='my_task')
def my_task():
     ...

Hope it helps!

6
votes

In Celery 3+:

from celery import Celery

app = Celery()
app.send_task('my_task', [], kwargs)