print statement inside celery task not working

Question:

I need to debug celery task to see whether it is working properly or not, So I put some print statements inside tasks.py. While running the project I have observed that task successfully runs but doesn’t print anything on the console. How can I debug the celery task.

Asked By: arush1836

||

Answers:

When Celery actually does the work, tasks are being handled by different Python processes. Depending on how you have things configured, those separate processes might have no access to your console. Instead, they will write to Celery’s log file (which can be configured in various ways). Find that file, and you’ll see print output from your tasks.

All of that assumes that you have indeed restarted the Celery workers, as suggested by the comment from Gaurav Tomer.

Answered By: FMc

For debug celery:

from celery import task
from celery.contrib import rdb

@task()
def add(x, y):
    result = x + y
    rdb.set_trace()  # <- set break-point
    return result

After running you will see this log:

[INFO/MainProcess] Received task:
    tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8]
[WARNING/PoolWorker-1] Remote Debugger:6900:
    Please telnet 127.0.0.1 6900.  Type `exit` in session to continue.
[2011-01-18 14:25:44,119: WARNING/PoolWorker-1] Remote Debugger:6900:
    Waiting for client...

You need to telnet to the port like below:

telnet localhost 6900

Now you can debug your task. more detail in celery.

Answered By: mastisa

For Windows use the command

celery -A <project_name>.celery worker --pool=solo -l INFO

restart celery worker with that command

Answered By: Karan wadhawan 25
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.