signals.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  20. kwargs=None, \*\*kwds):
  21. print("Got signal task_sent for task id %s" % (task_id, ))
  22. task_sent.connect(task_sent_handler)
  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. .. _signal-ref:
  31. Signals
  32. =======
  33. Task Signals
  34. ------------
  35. .. signal:: task_sent
  36. task_sent
  37. ~~~~~~~~~
  38. Dispatched when a task has been sent to the broker.
  39. Note that this is executed in the client process, the one sending
  40. the task, not in the worker.
  41. Sender is the name of the task being sent.
  42. Provides arguments:
  43. * task_id
  44. Id of the task to be executed.
  45. * task
  46. The task being executed.
  47. * args
  48. the tasks positional arguments.
  49. * kwargs
  50. The tasks keyword arguments.
  51. * eta
  52. The time to execute the task.
  53. * taskset
  54. Id of the taskset this task is part of (if any).
  55. .. signal:: task_prerun
  56. task_prerun
  57. ~~~~~~~~~~~
  58. Dispatched before a task is executed.
  59. Sender is the task class being executed.
  60. Provides arguments:
  61. * task_id
  62. Id of the task to be executed.
  63. * task
  64. The task being executed.
  65. * args
  66. the tasks positional arguments.
  67. * kwargs
  68. The tasks keyword arguments.
  69. .. signal:: task_postrun
  70. task_postrun
  71. ~~~~~~~~~~~~
  72. Dispatched after a task has been executed.
  73. Sender is the task class executed.
  74. Provides arguments:
  75. * task_id
  76. Id of the task to be executed.
  77. * task
  78. The task being executed.
  79. * args
  80. The tasks positional arguments.
  81. * kwargs
  82. The tasks keyword arguments.
  83. * retval
  84. The return value of the task.
  85. .. signal:: task_failure
  86. task_failure
  87. ~~~~~~~~~~~~
  88. Dispatched when a task fails.
  89. Sender is the task class executed.
  90. Provides arguments:
  91. * task_id
  92. Id of the task.
  93. * exception
  94. Exception instance raised.
  95. * args
  96. Positional arguments the task was called with.
  97. * kwargs
  98. Keyword arguments the task was called with.
  99. * traceback
  100. Stack trace object.
  101. * einfo
  102. The :class:`celery.datastructures.ExceptionInfo` instance.
  103. Worker Signals
  104. --------------
  105. .. signal:: worker_init
  106. worker_init
  107. ~~~~~~~~~~~
  108. Dispatched before the worker is started.
  109. .. signal:: worker_ready
  110. worker_ready
  111. ~~~~~~~~~~~~
  112. Dispatched when the worker is ready to accept work.
  113. .. signal:: worker_process_init
  114. worker_process_init
  115. ~~~~~~~~~~~~~~~~~~~
  116. Dispatched by each new pool worker process when it starts.
  117. .. signal:: worker_shutdown
  118. worker_shutdown
  119. ~~~~~~~~~~~~~~~
  120. Dispatched when the worker is about to shut down.
  121. Celerybeat Signals
  122. ------------------
  123. .. signal:: beat_init
  124. beat_init
  125. ~~~~~~~~~
  126. Dispatched when celerybeat starts (either standalone or embedded).
  127. Sender is the :class:`celery.beat.Service` instance.
  128. .. signal:: beat_embedded_init
  129. beat_embedded_init
  130. ~~~~~~~~~~~~~~~~~~
  131. Dispatched in addition to the :signal:`beat_init` signal when celerybeat is
  132. started as an embedded process. Sender is the
  133. :class:`celery.beat.Service` instance.
  134. Eventlet Signals
  135. ----------------
  136. .. signal:: eventlet_pool_started
  137. eventlet_pool_started
  138. ~~~~~~~~~~~~~~~~~~~~~
  139. Sent when the eventlet pool has been started.
  140. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  141. .. signal:: eventlet_pool_preshutdown
  142. eventlet_pool_preshutdown
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~
  144. Sent when the worker shutdown, just before the eventlet pool
  145. is requested to wait for remaining workers.
  146. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  147. .. signal:: eventlet_pool_postshutdown
  148. eventlet_pool_postshutdown
  149. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  150. Sent when the pool has been joined and the worker is ready to shutdown.
  151. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  152. .. signal:: eventlet_pool_apply
  153. eventlet_pool_apply
  154. ~~~~~~~~~~~~~~~~~~~
  155. Sent whenever a task is applied to the pool.
  156. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  157. Provides arguments:
  158. * target
  159. The target function.
  160. * args
  161. Positional arguments.
  162. * kwargs
  163. Keyword arguments.
  164. Logging Signals
  165. ---------------
  166. .. signal:: setup_logging
  167. setup_logging
  168. ~~~~~~~~~~~~~
  169. Celery won't configure the loggers if this signal is connected,
  170. so you can use this to completely override the logging configuration
  171. with your own.
  172. If you would like to augment the logging configuration setup by
  173. Celery then you can use the :signal:`after_setup_logger` and
  174. :signal:`after_setup_task_logger` signals.
  175. Provides arguments:
  176. * loglevel
  177. The level of the logging object.
  178. * logfile
  179. The name of the logfile.
  180. * format
  181. The log format string.
  182. * colorize
  183. Specify if log messages are colored or not.
  184. .. signal:: after_setup_logger
  185. after_setup_logger
  186. ~~~~~~~~~~~~~~~~~~
  187. Sent after the setup of every global logger (not task loggers).
  188. Used to augment logging configuration.
  189. Provides arguments:
  190. * logger
  191. The logger object.
  192. * loglevel
  193. The level of the logging object.
  194. * logfile
  195. The name of the logfile.
  196. * format
  197. The log format string.
  198. * colorize
  199. Specify if log messages are colored or not.
  200. .. signal:: after_setup_task_logger
  201. after_setup_task_logger
  202. ~~~~~~~~~~~~~~~~~~~~~~~
  203. Sent after the setup of every single task logger.
  204. Used to augment logging configuration.
  205. Provides arguments:
  206. * logger
  207. The logger object.
  208. * loglevel
  209. The level of the logging object.
  210. * logfile
  211. The name of the logfile.
  212. * format
  213. The log format string.
  214. * colorize
  215. Specify if log messages are colored or not.