Sid
Sid

Reputation: 71

Not able to see the output messages of the tasks performed in celery-rabbitMQ combination

I am trying out celery and rabbitMQ combinations for asynchronous task scheduling, below is the sample program I tried on Pycharm IDE....Everything seems to be working but I am not able to see the return value from the tasks.

I am also monitoring the rabbitMQ management console , but still not able to see the return value from the tasks.

I dont understand where I am going wrong,this is my first attempt at celery and RabbitMQ

I have created a tasks.py file with 2 sample tasks(with proper decorators assigned) and returning a value for each task.

I have also started teh RabbitMQ server (using {rabbitmq-server start} command).

Then I have started the celery worker, command used : {celery -A tasks --loglevel=info}

Now, when I am trying to execute these tasks using delay() method, the command ({reverse.delay('andy')}) is running and I am getting , something like this, but I am not able to see the returned value.

from celery import  Celery

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

@app.task
def reverse(string):
    return string[::-1]

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

Upvotes: 2

Views: 230

Answers (2)

Sid
Sid

Reputation: 71

Well, I have figured out the issue... It seams the latest versions of celery don't go well with windows. To fix this issue, I have installed 'eventlet' package and it takes care of the rest.

One thing to note is that, we need to start the celery worker using eventlet support, PFB the command:

celery -A <module_name> worker -loglevel=info -P eventlet

Upvotes: 1

2ps
2ps

Reputation: 15916

You can check the return value from task by using the result property. The syntax below is in python3 with type hinting so that you can follow along with what is going on.

import time
from celery.result import AsyncResult

result: AsyncResult = reverse.delay('andy')
task_id: str = result.id

while not result.ready():
    time.sleep(1)
    result = AsyncResult(task_id)
if result.successful():
   return_value = result.result

n.b., the above is a naive example because in a production environment, you typically don't busy wait like we did here. Instead the client issues periodic polls checking whether the status is ready and, if not, returns control to some other portion of the code (like a UI with a spinner).

Upvotes: 0

Related Questions