periodic-tasks.rst 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.decorators import periodic_task
  8. from datetime import timedelta
  9. @periodic_task(run_every=timedelta(seconds=30))
  10. def every_30_seconds(**kwargs):
  11. logger = self.get_logger(**kwargs)
  12. logger.info("Running periodic task!")
  13. If you want a little more control over when the task is executed, for example,
  14. a particular time of day or day of the week, you can use the ``crontab`` schedule
  15. type:
  16. .. code-block:: python
  17. from celery.task.schedules import crontab
  18. from celery.decorators import periodic_task
  19. @periodic_task(run_every=crontab(hour=7, minute=30, day_of_week=1))
  20. def every_monday_morning(**kwargs):
  21. logger = self.get_logger(**kwargs)
  22. logger.info("Execute every Monday at 7:30AM.")
  23. The syntax of these crontab expressions is very flexible. Some examples:
  24. +-------------------------------------+--------------------------------------------+
  25. | **Example** | **Meaning** |
  26. +-------------------------------------+--------------------------------------------+
  27. | crontab() | Execute every minute. |
  28. +-------------------------------------+--------------------------------------------+
  29. | crontab(minute=0, hour=0) | Execute daily at midnight. |
  30. +-------------------------------------+--------------------------------------------+
  31. | crontab(minute=0, | Execute every three hours---at midnight, |
  32. | | 3am, 6am, 9am, noon, 3pm, 6pm, 9pm. |
  33. +-------------------------------------+--------------------------------------------+
  34. | crontab(minute=0, | Same as previous. |
  35. | hour=[0,3,6,9,12,15,18,21]) | |
  36. +-------------------------------------+--------------------------------------------+
  37. | crontab(minute="\*/15") | Execute every 15 minutes. |
  38. +-------------------------------------+--------------------------------------------+
  39. | crontab(day_of_week="sunday") | Execute every minute (!) at sundays. |
  40. +-------------------------------------+--------------------------------------------+
  41. | crontab(minute="*", | Same as previous. |
  42. | hour="*", | |
  43. | day_of_week="sun") | |
  44. +-------------------------------------+--------------------------------------------+
  45. | crontab(minute="\*/10", | Execute every ten minutes, but only |
  46. | hour="3,17,22", | between 3-4 am, 5-6 pm and 10-11 pm on |
  47. | day_of_week="thu,fri") | thursdays or fridays. |
  48. +-------------------------------------+--------------------------------------------+
  49. | crontab(minute=0, hour="\*/2,\*/3") | Execute every even hour, and every hour |
  50. | | divisable by three. This means: |
  51. | | at every hour *except*: 1am, |
  52. | | 5am, 7am, 11am, 1pm, 5pm, 7pm, |
  53. | | 11pm |
  54. +-------------------------------------+--------------------------------------------+
  55. | crontab(minute=0, hour="\*/5") | Execute hour divisable by 5. This means |
  56. | | that it is triggered at 3pm, not 5pm |
  57. | | (since 3pm equals the 24-hour clock |
  58. | | value of "15", which is divisable by 5). |
  59. +-------------------------------------+--------------------------------------------+
  60. | crontab(minute=0, hour="\*/3,8-17") | Execute every hour divisable by 3, and |
  61. | | every hour during office hours (8am-5pm). |
  62. +-------------------------------------+--------------------------------------------+
  63. If you want to use periodic tasks you need to start the ``celerybeat``
  64. service. You have to make sure only one instance of this server is running at
  65. any time, or else you will end up with multiple executions of the same task.
  66. To start the ``celerybeat`` service::
  67. $ celerybeat
  68. You can also start ``celerybeat`` with ``celeryd`` by using the ``-B`` option,
  69. this is convenient if you only have one server::
  70. $ celeryd -B