Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery tasks without result write to result backend

I have some tasks which should return result, and some tasks that don't. I want to force tasks which shouldn't return result not to write anything in result backend (for example None). How can I achieve that in Celery?

For example it's my tasks:

@app.task
def taskWithResult():
    # ...code...
    return res

@app.task
def taskWithNoResult():
    # ...code without return...

And also I have special queue for some others task which also don't return any result, can I mark that queue as with tasks which mustn't write in result backend?

like image 998
Sergey Luchko Avatar asked Nov 25 '16 16:11

Sergey Luchko


People also ask

What is Celery result backend?

Celery uses a result backend to keep track of the tasks' states. In the previous tutorial, we saw how Celery works and how to integrate it into a Django application. In this tutorial, we are going to use the RPC (RabbitMQ/AMQP) result backend to store and retrieve the states of tasks.

What happens when a celery task fails?

Celery will stop retrying after 7 failed attempts and raise an exception.

How does Celery execute tasks?

Process of Task Execution by Celery can be broken down into:Your application sends the tasks to the task broker, it is then reserved by a worker for execution & finally the result of task execution is stored in the result backend.


2 Answers

Global Config

app.conf.task_ignore_result = True

Locally close return task results:

@app.task(ignore_result=True)
def add(...):

If you only want to return and persist the abnormal results of the task execution failure for subsequent investigation and analysis, then you can apply the following configuration while using the database as a Result Backend:

Only store task errors in the result backend.

app.conf.task_ignore_result = True
app.conf.task_store_errors_even_if_ignored = True
like image 175
Anand Tripathi Avatar answered Oct 16 '22 23:10

Anand Tripathi


From celery document you can set ignore result flag to True. http://docs.celeryproject.org/en/latest/reference/celery.app.task.html?highlight=default_retry_delay#celery.app.task.Task.ignore_result

For example:

@app.task(ignore_result=True)
def taskWithNoResult():
    # ...code without return..
like image 20
kkiat Avatar answered Oct 16 '22 23:10

kkiat