How to manually mark a Celery task as done and set its result?

2024/10/15 13:24:49

I have this Celery task:

@app.task
def do_something(with_this):# instantiate a class from a third party libraryinstance = SomeClass()# this class uses callbacks to send progress info about# the status and progress of what we're doingdef progress_callback(data):# this status will change to 'finished' later# but the return value that I want as the task result won't be returned# so this is where I should mark the task as done manuallyif data['status'] == 'working':# I create a custom state for this taskdo_something.update_state(state = 'PROGRESS',meta = data['progress'])# adding the callback to the instanceinstance.add_callback(progress_callback)# use the instance to do what I want# this functions returns a value that I don't want as the task result# so leaving this function without a return statement will make it Noneinstance.do_job(with_this)

How can I mark a task as done manually ?

In this case the function reaches the end without any return statement so the task.result I get is None, I want to set the data passed to the callback function as the result and mark the task as done.

I tried using:

app.backend.mark_as_done(do_something.request.id, data)

It's successfully setting the state and the result of the task but later the result is set to the return value of the function which is here None.

Answer

I finally found the solution which is storing the task state and result then ignoring the task by raising an Ignore exception, for example:

from celery.exceptions import Ignore@app.task
def do_something(with_this):# store the state and result manually# the SUCCESS state is set by this methodapp.backend.mark_as_done(do_something.request.id,the_data_to_store)# we can also use update_state which calls# backend.store_result just like mark_as_done# but we have to set the state in this casedo_something.update_state(state = celery.states.SUCCESS,meta = the_data_to_store)# ignore the task so no other state is recorded# like what was happening with my function in the question# the task will still be acknowledgedraise Ignore()

This is helpful when you cannot return the data that you want to store as the result.

https://en.xdnf.cn/q/69280.html

Related Q&A

How to sort a numpy array based on the values in a specific row?

I was wondering how I would be able to sort a whole array by the values in one of its columns.I have :array([5,2,8,2,4])and:array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14],[15, 16…

python regex match optional square brackets

I have the following strings:1 "R J BRUCE & OTHERS V B J & W L A EDWARDS And Ors CA CA19/02 27 February 2003", 2 "H v DIRECTOR OF PROCEEDINGS [2014] NZHC 1031 [16 May 2014]&…

How to open console in firefox python selenium?

Im trying to open firefox console through Selenium with Python. How can I open firefox console with python selenium? Is it possible to send keys to the driver or something like that?

Can python coverage module conditionally ignore lines in a unit test?

Using nosetests and the coverage module, I would like coverage reports for code to reflect the version being tested. Consider this code:import sys if sys.version_info < (3,3):print(older version of …

Delete Pandas DataFrame row where column value is 0

I already read the answers in this thread but it doesnt answer my exact problem. My DataFrame looks like thisLady in the Water The Night Listener Just My Luck Correlation Claudia Puig …

Pyarrow s3fs partition by timestamp

Is it possible to use a timestamp field in the pyarrow table to partition the s3fs file system by "YYYY/MM/DD/HH" while writing parquet file to s3?

flask run vs. python

Im having difficulty getting my flask app to run by using the "python" method. I have no problems usingexport FLASK_APP=microblog.py flask runbut attempting to usepython microblog.pywill resu…

Pandas-Add missing years in time series data with duplicate years

I have a dataset like this where data for some years are missing .County Year Pop 12 1999 1.1 12 2001 1.2 13 1999 1.0 13 2000 1.1I want something like County Year Pop 12 1999 1.1 12…

Saving zip list to csv in Python

How I can write below zip list to csv file in python?[{date: 2015/01/01 00:00, v: 96.5},{date: 2015/01/01 00:01, v: 97.0},{date: 2015/01/01 00:02, v: 93.75},{date: 2015/01/01 00:03, v: 96.0},{date: 20…

unable to download the pipeline provided by spark-nlp library

i am unable to use the predefined pipeline "recognize_entities_dl" provided by the spark-nlp library i tried installing different versions of pyspark and spark-nlp libraryimport sparknlp from…