signals.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. .. _signals:
  2. =======
  3. Signals
  4. =======
  5. .. contents::
  6. :local:
  7. Signals allows decoupled applications to receive notifications when
  8. certain actions occur elsewhere in the application.
  9. Celery ships with many signals that you application can hook into
  10. to augment behavior of certain actions.
  11. .. _signal-basics:
  12. Basics
  13. ======
  14. Several kinds of events trigger signals, you can connect to these signals
  15. to perform actions as they trigger.
  16. Example connecting to the :signal:`task_sent` signal:
  17. .. code-block:: python
  18. from celery.signals import task_sent
  19. @task_sent.connect
  20. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  21. kwargs=None, \*\*kwds):
  22. print("Got signal task_sent for task id %s" % (task_id, ))
  23. Some signals also have a sender which you can filter by. For example the
  24. :signal:`task_sent` signal uses the task name as a sender, so you can
  25. connect your handler to be called only when tasks with name `"tasks.add"`
  26. has been sent by providing the `sender` argument to
  27. :class:`~celery.utils.dispatch.signal.Signal.connect`:
  28. .. code-block:: python
  29. @task_sent.connect(task_sent_handler, sender="tasks.add")
  30. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  31. kwargs=None, \*\*kwds):
  32. print("Got signal task_sent for task id %s" % (task_id, ))
  33. .. _signal-ref:
  34. Signals
  35. =======
  36. Task Signals
  37. ------------
  38. .. signal:: task_sent
  39. task_sent
  40. ~~~~~~~~~
  41. Dispatched when a task has been sent to the broker.
  42. Note that this is executed in the client process, the one sending
  43. the task, not in the worker.
  44. Sender is the name of the task being sent.
  45. Provides arguments:
  46. * task_id
  47. Id of the task to be executed.
  48. * task
  49. The task being executed.
  50. * args
  51. the tasks positional arguments.
  52. * kwargs
  53. The tasks keyword arguments.
  54. * eta
  55. The time to execute the task.
  56. * taskset
  57. Id of the taskset this task is part of (if any).
  58. .. signal:: task_prerun
  59. task_prerun
  60. ~~~~~~~~~~~
  61. Dispatched before a task is executed.
  62. Sender is the task class being executed.
  63. Provides arguments:
  64. * task_id
  65. Id of the task to be executed.
  66. * task
  67. The task being executed.
  68. * args
  69. the tasks positional arguments.
  70. * kwargs
  71. The tasks keyword arguments.
  72. .. signal:: task_postrun
  73. task_postrun
  74. ~~~~~~~~~~~~
  75. Dispatched after a task has been executed.
  76. Sender is the task class executed.
  77. Provides arguments:
  78. * task_id
  79. Id of the task to be executed.
  80. * task
  81. The task being executed.
  82. * args
  83. The tasks positional arguments.
  84. * kwargs
  85. The tasks keyword arguments.
  86. * retval
  87. The return value of the task.
  88. .. signal:: task_failure
  89. task_failure
  90. ~~~~~~~~~~~~
  91. Dispatched when a task fails.
  92. Sender is the task class executed.
  93. Provides arguments:
  94. * task_id
  95. Id of the task.
  96. * exception
  97. Exception instance raised.
  98. * args
  99. Positional arguments the task was called with.
  100. * kwargs
  101. Keyword arguments the task was called with.
  102. * traceback
  103. Stack trace object.
  104. * einfo
  105. The :class:`celery.datastructures.ExceptionInfo` instance.
  106. Worker Signals
  107. --------------
  108. .. signal:: celeryd_init
  109. celeryd_init
  110. ~~~~~~~~~~~~
  111. This is the first signal sent when :program:`celeryd` starts up.
  112. The ``sender`` is the host name of the worker, so this signal can be used
  113. to setup worker specific configuration:
  114. .. code-block:: python
  115. from celery.signals import celeryd_init
  116. @celeryd_init.connect(sender="worker12.example.com")
  117. def configure_worker12(conf=None, **kwargs):
  118. conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
  119. or to set up configuration for multiple workers you can omit specifying a
  120. sender when you connect:
  121. .. code-block:: python
  122. from celery.signals import celeryd_init
  123. @celeryd_init.connect
  124. def configure_workers(sender=None, conf=None, **kwargs):
  125. if sender in ("worker1.example.com", "worker2.example.com"):
  126. conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
  127. if sender == "worker3.example.com":
  128. conf.CELERYD_PREFETCH_MULTIPLIER = 0
  129. Provides arguments:
  130. * instance
  131. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  132. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  133. set so far, and the rest of ``__init__`` has not been executed.
  134. * conf
  135. The configuration of the current app.
  136. .. signal:: worker_init
  137. worker_init
  138. ~~~~~~~~~~~
  139. Dispatched before the worker is started.
  140. .. signal:: worker_ready
  141. worker_ready
  142. ~~~~~~~~~~~~
  143. Dispatched when the worker is ready to accept work.
  144. .. signal:: worker_process_init
  145. worker_process_init
  146. ~~~~~~~~~~~~~~~~~~~
  147. Dispatched by each new pool worker process when it starts.
  148. .. signal:: worker_shutdown
  149. worker_shutdown
  150. ~~~~~~~~~~~~~~~
  151. Dispatched when the worker is about to shut down.
  152. Celerybeat Signals
  153. ------------------
  154. .. signal:: beat_init
  155. beat_init
  156. ~~~~~~~~~
  157. Dispatched when celerybeat starts (either standalone or embedded).
  158. Sender is the :class:`celery.beat.Service` instance.
  159. .. signal:: beat_embedded_init
  160. beat_embedded_init
  161. ~~~~~~~~~~~~~~~~~~
  162. Dispatched in addition to the :signal:`beat_init` signal when celerybeat is
  163. started as an embedded process. Sender is the
  164. :class:`celery.beat.Service` instance.
  165. Eventlet Signals
  166. ----------------
  167. .. signal:: eventlet_pool_started
  168. eventlet_pool_started
  169. ~~~~~~~~~~~~~~~~~~~~~
  170. Sent when the eventlet pool has been started.
  171. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  172. .. signal:: eventlet_pool_preshutdown
  173. eventlet_pool_preshutdown
  174. ~~~~~~~~~~~~~~~~~~~~~~~~~
  175. Sent when the worker shutdown, just before the eventlet pool
  176. is requested to wait for remaining workers.
  177. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  178. .. signal:: eventlet_pool_postshutdown
  179. eventlet_pool_postshutdown
  180. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  181. Sent when the pool has been joined and the worker is ready to shutdown.
  182. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  183. .. signal:: eventlet_pool_apply
  184. eventlet_pool_apply
  185. ~~~~~~~~~~~~~~~~~~~
  186. Sent whenever a task is applied to the pool.
  187. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  188. Provides arguments:
  189. * target
  190. The target function.
  191. * args
  192. Positional arguments.
  193. * kwargs
  194. Keyword arguments.
  195. Logging Signals
  196. ---------------
  197. .. signal:: setup_logging
  198. setup_logging
  199. ~~~~~~~~~~~~~
  200. Celery won't configure the loggers if this signal is connected,
  201. so you can use this to completely override the logging configuration
  202. with your own.
  203. If you would like to augment the logging configuration setup by
  204. Celery then you can use the :signal:`after_setup_logger` and
  205. :signal:`after_setup_task_logger` signals.
  206. Provides arguments:
  207. * loglevel
  208. The level of the logging object.
  209. * logfile
  210. The name of the logfile.
  211. * format
  212. The log format string.
  213. * colorize
  214. Specify if log messages are colored or not.
  215. .. signal:: after_setup_logger
  216. after_setup_logger
  217. ~~~~~~~~~~~~~~~~~~
  218. Sent after the setup of every global logger (not task loggers).
  219. Used to augment logging configuration.
  220. Provides arguments:
  221. * logger
  222. The logger object.
  223. * loglevel
  224. The level of the logging object.
  225. * logfile
  226. The name of the logfile.
  227. * format
  228. The log format string.
  229. * colorize
  230. Specify if log messages are colored or not.
  231. .. signal:: after_setup_task_logger
  232. after_setup_task_logger
  233. ~~~~~~~~~~~~~~~~~~~~~~~
  234. Sent after the setup of every single task logger.
  235. Used to augment logging configuration.
  236. Provides arguments:
  237. * logger
  238. The logger object.
  239. * loglevel
  240. The level of the logging object.
  241. * logfile
  242. The name of the logfile.
  243. * format
  244. The log format string.
  245. * colorize
  246. Specify if log messages are colored or not.