How can I run a celery periodic task from the shell manually?

Mridang Agarwalla picture Mridang Agarwalla · Oct 15, 2012 · Viewed 41.9k times · Source

I'm using celery and django-celery. I have defined a periodic task that I'd like to test. Is it possible to run the periodic task from the shell manually so that I view the console output?

Answer

Platinum Azure picture Platinum Azure · Oct 15, 2012

Have you tried just running the task from the Django shell? You can use the .apply method of a task to ensure that it is run eagerly and locally.

Assuming the task is called my_task in Django app myapp in a tasks submodule:

$ python manage.py shell
>>> from myapp.tasks import my_task
>>> eager_result = my_task.apply()

The result instance has the same API as the usual AsyncResult type, except that the result is always evaluated eagerly and locally and the .apply() method will block until the task is run to completion.