Fabbree
Fabbree

Reputation: 51

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

Upvotes: 5

Views: 1581

Answers (2)

cedrik
cedrik

Reputation: 559

Just starting with Celery, so I don't know why !!

def OK():
    print("OK")

add.delay(3, 5).then(OK())  # Works
add.delay(3, 5).then(lambda: print("OK"))  # Fails...

Upvotes: 0

alanjds
alanjds

Reputation: 4130

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.

Upvotes: 1

Related Questions