问题
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).
回答1:
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!
回答2:
In Celery 3+:
from celery import Celery
app = Celery()
app.send_task('my_task', [], kwargs)
来源:https://stackoverflow.com/questions/19239154/call-django-celery-task-by-name