Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a callback in celery

Tags:

python

celery

I want to add callback to a function so that when it returns it can call a regular python function,

My Task

@celery.task                                                                                                                            
def add(x, y):                                                                                                                         
    return x + y 

How I want to use it:

from __future__ import print_function
delay.add(2, 3 ,callback=lambda x: print x) 

Instead of the lambda it can be any function that its not defined in the celery tasks but where the task is called from.

like image 847
Totic Avatar asked Sep 14 '12 12:09

Totic


People also ask

What does bind true do in Celery?

The bind argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance.

How do you pass arguments to Celery task?

To pass arguments to task with apply_async() you need to wrap them in a list and then pass the list as first argument, I.e. apply_async([arg1, arg2, arg3]) . See the documentation for more details and examples. Use delay() as an alternative.

How does Celery retry work?

The first retry will have a delay of 1 second, the second retry will have a delay of 2 seconds, the third will delay 4 seconds, the fourth will delay 8 seconds, and so on. (However, this delay value is modified by retry_jitter , if it is enabled.) If this option is set to a number, it is used as a delay factor.


1 Answers

You can only link tasks in this case:

add.apply_async((2, 3), link=other_task.s())

which is the same as:

(add.s(2, 3) | other_task.s())()

Waiting for the task to complete makes the task synchronous, so the call you want would be equivalent to:

(lambda x: print(x))(add.delay(2, 3).get())

Which would block the current process until the task returns. If you don't want the process to block then you would have to write a dedicated thread to wait for results and calling your callbacks.

Or you can use eventlet/gevent and pretty much write normal code.

like image 136
asksol Avatar answered Sep 30 '22 15:09

asksol