signals.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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:: celeryd_init
  106. celeryd_init
  107. ~~~~~~~~~~~~
  108. This is the first signal sent when :program:`celeryd` starts up.
  109. The ``sender`` is the host name of the worker, so this signal can be used
  110. to setup worker specific configuration:
  111. .. code-block:: python
  112. from celery.signals import celeryd_init
  113. @celeryd_init.connect(sender="worker12.example.com")
  114. def configure_worker12(conf=None, **kwargs):
  115. conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
  116. or to set up configuration for multiple workers you can omit specifying a
  117. sender when you connect:
  118. .. code-block:: python
  119. from celery.signals import celeryd_init
  120. @celeryd_init.connect
  121. def configure_workers(sender=None, conf=None, **kwargs):
  122. if sender in ("worker1.example.com", "worker2.example.com"):
  123. conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
  124. if sender == "worker3.example.com":
  125. conf.CELERYD_PREFETCH_MULTIPLIER = 0
  126. Provides arguments:
  127. * instance
  128. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  129. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  130. set so far, and the rest of ``__init__`` has not been executed.
  131. * conf
  132. The configuration of the current app.
  133. .. signal:: worker_init
  134. worker_init
  135. ~~~~~~~~~~~
  136. Dispatched before the worker is started.
  137. .. signal:: worker_ready
  138. worker_ready
  139. ~~~~~~~~~~~~
  140. Dispatched when the worker is ready to accept work.
  141. .. signal:: worker_process_init
  142. worker_process_init
  143. ~~~~~~~~~~~~~~~~~~~
  144. Dispatched by each new pool worker process when it starts.
  145. .. signal:: worker_shutdown
  146. worker_shutdown
  147. ~~~~~~~~~~~~~~~
  148. Dispatched when the worker is about to shut down.
  149. Celerybeat Signals
  150. ------------------
  151. .. signal:: beat_init
  152. beat_init
  153. ~~~~~~~~~
  154. Dispatched when celerybeat starts (either standalone or embedded).
  155. Sender is the :class:`celery.beat.Service` instance.
  156. .. signal:: beat_embedded_init
  157. beat_embedded_init
  158. ~~~~~~~~~~~~~~~~~~
  159. Dispatched in addition to the :signal:`beat_init` signal when celerybeat is
  160. started as an embedded process. Sender is the
  161. :class:`celery.beat.Service` instance.
  162. Eventlet Signals
  163. ----------------
  164. .. signal:: eventlet_pool_started
  165. eventlet_pool_started
  166. ~~~~~~~~~~~~~~~~~~~~~
  167. Sent when the eventlet pool has been started.
  168. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  169. .. signal:: eventlet_pool_preshutdown
  170. eventlet_pool_preshutdown
  171. ~~~~~~~~~~~~~~~~~~~~~~~~~
  172. Sent when the worker shutdown, just before the eventlet pool
  173. is requested to wait for remaining workers.
  174. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  175. .. signal:: eventlet_pool_postshutdown
  176. eventlet_pool_postshutdown
  177. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  178. Sent when the pool has been joined and the worker is ready to shutdown.
  179. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  180. .. signal:: eventlet_pool_apply
  181. eventlet_pool_apply
  182. ~~~~~~~~~~~~~~~~~~~
  183. Sent whenever a task is applied to the pool.
  184. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  185. Provides arguments:
  186. * target
  187. The target function.
  188. * args
  189. Positional arguments.
  190. * kwargs
  191. Keyword arguments.
  192. Logging Signals
  193. ---------------
  194. .. signal:: setup_logging
  195. setup_logging
  196. ~~~~~~~~~~~~~
  197. Celery won't configure the loggers if this signal is connected,
  198. so you can use this to completely override the logging configuration
  199. with your own.
  200. If you would like to augment the logging configuration setup by
  201. Celery then you can use the :signal:`after_setup_logger` and
  202. :signal:`after_setup_task_logger` signals.
  203. Provides arguments:
  204. * loglevel
  205. The level of the logging object.
  206. * logfile
  207. The name of the logfile.
  208. * format
  209. The log format string.
  210. * colorize
  211. Specify if log messages are colored or not.
  212. .. signal:: after_setup_logger
  213. after_setup_logger
  214. ~~~~~~~~~~~~~~~~~~
  215. Sent after the setup of every global logger (not task loggers).
  216. Used to augment logging configuration.
  217. Provides arguments:
  218. * logger
  219. The logger object.
  220. * loglevel
  221. The level of the logging object.
  222. * logfile
  223. The name of the logfile.
  224. * format
  225. The log format string.
  226. * colorize
  227. Specify if log messages are colored or not.
  228. .. signal:: after_setup_task_logger
  229. after_setup_task_logger
  230. ~~~~~~~~~~~~~~~~~~~~~~~
  231. Sent after the setup of every single task logger.
  232. Used to augment logging configuration.
  233. Provides arguments:
  234. * logger
  235. The logger object.
  236. * loglevel
  237. The level of the logging object.
  238. * logfile
  239. The name of the logfile.
  240. * format
  241. The log format string.
  242. * colorize
  243. Specify if log messages are colored or not.