How to make celery retry using the same worker?

匆匆过客 提交于 2019-12-08 20:28:50

问题


I'm just starting out with celery in a Django project, and am kinda stuck at this particular problem: Basically, I need to distribute a long-running task to different workers. The task is actually broken into several steps, each of which takes considerable time to complete. Therefore, if some step fails, I'd like celery to retry this task using the same worker to reuse the results from the completed steps. I understand that celery uses routing to distribute tasks to certain server, but I can't find anything about this particular problem. I use RabbitMQ as my broker.


回答1:


You could have every celeryd instance consume from a queue named after the hostname of the worker:

celeryd -l info -n worker1.example.com  -Q celery,worker1.example.com

sets the hostname to worker1.example.com and will consume from a queue named the same, as well as the default queue (named celery).

Then to direct a task to a specific worker you can use:

task.apply_async(args, kwargs, queue="worker1.example.com")

similary to direct a retry:

task.retry(queue="worker1.example.com")

or to direct the retry to the same worker:

task.retry(queue=task.request.hostname)


来源:https://stackoverflow.com/questions/8753216/how-to-make-celery-retry-using-the-same-worker

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