Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery 4.0.2 AsyncResult.then not working

I would like to adopt the promise protocol as shown in the docs. The example provided there works but the promise is handled on the worker side. Instead I would like to get notified on the client.

Here is my test.py:

from celery import Celery

app = Celery(broker='amqp://', backend='rpc')

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

On the client side I'm entering the following commands:

import test
test.add.delay(2, 2).then(lambda: print('OK'))

While googling around I've bump into this so apparently I'm not the only one struggling to understand how it is supposed to work.

My understanding is that once the task has been processed, result should be sent back to the client and then the callback should fire, but that's not the case, my promise never gets resolved.

Is my understanding correct? Is this the desired behaviour?

Thx

like image 901
Fabbree Avatar asked Nov 07 '22 20:11

Fabbree


1 Answers

Depending on the backend, the "check" of the resolution does not occur automatically. You need to actively .ready() or .wait() for it. You may want to defer this check to a side thread or so.

On amqp backend, it will resolve when you try to .ready() the AsyncResult. Then it means pooling for resolution. Somewhere, I had read that redis backend does the resolution without pooling, but had not yet dig on its code.

I am implementing a CeleryExecutor alike a ThreadPoolExecutor, and had to put .ready() checks all around to trigger the Future resolution.

like image 85
alanjds Avatar answered Nov 14 '22 22:11

alanjds