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

56,366

Solution 1

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.

Solution 2

If you means just trigger a task when the condition is not satisfied, for example, the periodic time not meet. You can do it in two steps.

1.Get your task id.

You can do it by typing.

celery inspect registered

You will see something like app.tasks.update_something. If nothing, it is probably that celery was not started. Just run it.

2.Run the task with celery call

celery call app.tasks.update_something

For more details, just type

celery --help
celery inspect --help
celery call --help

Solution 3

I think you'll need to open two shells: one for executing tasks from the Python/Django shell, and one for running celery worker (python manage.py celery worker). And as the previous answer said, you can run tasks using apply() or apply_async()

I've edited the answer so you're not using a deprecated command.

Share:
56,366
Mridang Agarwalla
Author by

Mridang Agarwalla

I'm a software developer who relishes authoring Java and Python, hacking on Android and toying with AppEngine. I have a penchant for development and a passion for the business side of software. In between all the work, I contribute to a number of open-source projects, learn to master the art of cooking Asian cuisine and try to stay sane while learning to fly my Align Trex-600 Nitro Heli.

Updated on July 08, 2022

Comments

  • Mridang Agarwalla
    Mridang Agarwalla almost 2 years

    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?

  • Workonphp
    Workonphp over 10 years
    How can I do the same for pyramid project? The directory structure will be like: /myproject=> celeryconfig.py, setup.py, development.py, /views/celerytasks=> mycelerytask.py .Now here I want to run 'mycelerytask.py'(which is periodic task) manually from command line. Can you help?
  • Platinum Azure
    Platinum Azure over 10 years
    @Workonphp I'm not familiar with Pyramid, unfortunately, so I don't know how Celery integrates with Pyramid at all. (Note that my solution above is Django-specific, requiring the manage.py script Django generates in all Django project directories.) Sorry.
  • rschwieb
    rschwieb about 9 years
    manage.py celeryd is deprecated now: stackoverflow.com/a/23921568/1459594
  • AlonS
    AlonS almost 5 years
    it also supports args and kwargs $ celery -A yourapp call app.tasks.update_something --kwargs='{"key": value,...}
  • Erik Kalkoken
    Erik Kalkoken over 4 years
    This is very helpful, but missing a parenthesis at the end. Corrected: celery -A yourapp call app.tasks.update_something --kwargs='{"key": value,...}'
  • ‌‌R‌‌‌.
    ‌‌R‌‌‌. about 4 years
    Although inspect is great, unfortunately it is only available for "RabbitMQ (AMQP) and Redis transports." (not others like filesystem for instance)