periodic-tasks.rst 4.8 KB

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