periodic-tasks.rst 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ================
  2. Periodic Tasks
  3. ================
  4. You can schedule tasks to run at intervals like ``cron``.
  5. Here's an example of a periodic task:
  6. .. code-block:: python
  7. from celery.task import PeriodicTask
  8. from celery.registry import tasks
  9. from datetime import timedelta
  10. class MyPeriodicTask(PeriodicTask):
  11. run_every = timedelta(seconds=30)
  12. def run(self, **kwargs):
  13. logger = self.get_logger(**kwargs)
  14. logger.info("Running periodic task!")
  15. >>> tasks.register(MyPeriodicTask)
  16. If you want to use periodic tasks you need to start the ``celerybeat``
  17. service. You have to make sure only one instance of this server is running at
  18. any time, or else you will end up with multiple executions of the same task.
  19. To start the ``celerybeat`` service::
  20. $ celerybeat
  21. or if using Django::
  22. $ python manage.py celerybeat
  23. You can also start ``celerybeat`` with ``celeryd`` by using the ``-B`` option,
  24. this is convenient if you only have one server::
  25. $ celeryd -B
  26. or if using Django::
  27. $ python manage.py celeryd -B