How to debug Celery/Django tasks running locally in Eclipse

Question:

I need to debug Celery task from the Eclipse debugger.
I’m using Eclipse, PyDev and Django.

First, I open my project in Eclipse and put a breakpoint at the beginning of the task function.

Then, I’m starting the Celery workers from Eclipse by Right Clicking on manage.py from the PyDev Package Explorer and choosing “Debug As->Python Run” and specifying “celeryd -l info” as the argument. This starts MainThread, Mediator and three more threads visible from the Eclipse debugger.

After that I return back to the PyDev view and start the main application by Right Click on the project and choosing Run As/PyDev:Django

My issues is that once the task is submitted by the mytask.delay() it doesn’t stop on the breakpoint. I put some traces withing the tasks code so I can see that it was executed in one of the worker threads.

So, how to make the Eclipse debugger to stop on the breakpoint placed withing the task when it executed in the Celery workers thread?

Asked By: spoonboy

||

Answers:

You should consider the option to run the celery task in the same thread as the main process (normally it runs on a separate process), this will make the debug much easier.

You can tell celery to run the task in sync by adding this setting to your settings.py module:

CELERY_TASK_ALWAYS_EAGER  = True
# use this if you are on older versions of celery
# CELERY_ALWAYS_EAGER = True 

Note: this is only meant to be in use for debugging or development stages!

Answered By: Tommaso Barbugli

If it runs only on a different thread, it should work on the latest PyDev versions (I think there was an issue before where a spawned thread would not be debugged, but this was fixed).

Now, if it’s launching on a different process, you need to use the remote debugger (even if it’s on the same machine). See: http://pydev.org/manual_adv_remote_debugger.html

Answered By: Fabio Zadrozny

CELERYD_POOL defaults to celery.concurrency.prefork:TaskPool which will spawn separate processes for each worker and PyDev can’t see inside them. If you change it to one of the threaded options then you can use the debugger.

For example, for Celery 3.1 you can use this setting:

CELERYD_POOL = 'celery.concurrency.threads:TaskPool'

Note that this requires the threadpool module to be installed.

Also make sure to have CELERY_ALWAYS_EAGER = False, otherwise changing the pool class makes no sense.

Answered By: sherbang

I create a management command to test task.. find it easier than running it from shell..

Answered By: eugene

You can do it using Celery’s rdb:

from celery.contrib import rdb
rdb.set_trace()

Then, in a different terminal type telnet localhost 6900, and you will get the debug prompt.

Answered By: seddonym
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.