Call Django celery task by name

只谈情不闲聊 提交于 2020-04-08 09:25:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!