How to make celery retry using the same worker?

2024/11/15 10:54:04

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.

Answer

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://en.xdnf.cn/q/71788.html

Related Q&A

Make an AJAX call to pass drop down value to the python script

I want to pass the selected value from dropdown which contains names of databases and pass it to the python script in the background which connects to the passed database name. Following is the ajax co…

PyLint 1.0.0 with PyDev + Eclipse: include-ids option no longer allowed, breaks Eclipse integration

As noted in this question: How do I get Pylint message IDs to show up after pylint-1.0.0?pylint 1.0.0 no longer accepts "include-ids" option. (It returns "lint.py: error: no such optio…

Shifting all rows in dask dataframe

In Pandas, there is a method DataFrame.shift(n) which shifts the contents of an array by n rows, relative to the index, similarly to np.roll(a, n). I cant seem to find a way to get a similar behaviour …

Pandas dataframe: omit weekends and days near holidays

I have a Pandas dataframe with a DataTimeIndex and some other columns, similar to this:import pandas as pd import numpy as nprange = pd.date_range(2017-12-01, 2018-01-05, freq=6H) df = pd.DataFrame(ind…

How to dump a boolean matrix in numpy?

I have a graph represented as a numpy boolean array (G.adj.dtype == bool). This is homework in writing my own graph library, so I cant use networkx. I want to dump it to a file so that I can fiddle wit…

Cant append_entry FieldList in Flask-wtf more than once

I have a form with flask-wtf for uploading images, also file field can be multiple fields. my form: class ComposeForm(Form):attachment = FieldList(FileField(_(file)), _(attachment))add_upload = SubmitF…

What is the best way to use python code from Scala (or Java)? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Java Python Integration There is some code written in Python and I need to use it from Scala. The code uses some native C.…

Pandas groupby week given a datetime column

Lets say I have the following data sample:df = pd.DataFrame({date:[2011-01-01,2011-01-02,2011-01-03,2011-01-04,2011-01-05,2011-01-06,2011-01-07,2011-01-08,2011-01-09,2011-12-30,2011-12-31],revenue:[5,3…

Django form to indicate input type

Another basic question Im afraid which Im struggling with. Ive been through the various Django documentation pages and also search this site. The only thing I have found on here was back in 2013 which…

run multi command in the same jupyter cells

Im trying to display 2 output of 2 lines in the same time, I use Panda library and it seems like it display only the output of second line:import pandas as pd data = {"state": ["Ohio&quo…