signals.rst 7.8 KB

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