celery.signals.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. .. _signals:
  2. ========================================
  3. Signals - celery.signals
  4. ========================================
  5. .. contents::
  6. :local:
  7. .. currentmodule:: celery.signals
  8. .. _signal-basics:
  9. Basics
  10. ======
  11. Several kinds of events trigger signals, you can connect to these signals
  12. to perform actions as they trigger.
  13. Example connecting to the :data:`task_sent` signal:
  14. .. code-block:: python
  15. from celery.signals import task_sent
  16. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  17. kwargs=None, **kwds):
  18. print("Got signal task_sent for task id %s" % (task_id, ))
  19. task_sent.connect(task_sent_handler)
  20. Some signals also have a sender which you can filter by. For example the
  21. :data:`task_sent` signal uses the task name as a sender, so you can
  22. connect your handler to be called only when tasks with name ``"tasks.add"``
  23. has been sent by providing the ``sender`` argument to
  24. :class:`~celery.utils.dispatch.signal.Signal.connect`:
  25. .. code-block:: python
  26. task_sent.connect(task_sent_handler, sender="tasks.add")
  27. .. _signal-ref:
  28. Signals
  29. =======
  30. Task Signals
  31. ------------
  32. .. data:: task_sent
  33. Dispatched when a task has been sent to the broker.
  34. Note that this is executed in the client process, the one sending
  35. the task, not in the worker.
  36. Sender is the name of the task being sent.
  37. Provides arguments:
  38. * task_id
  39. Id of the task to be executed.
  40. * task
  41. The task being executed.
  42. * args
  43. the tasks positional arguments.
  44. * kwargs
  45. The tasks keyword arguments.
  46. * eta
  47. The time to execute the task.
  48. * taskset
  49. Id of the taskset this task is part of (if any).
  50. .. data:: task_prerun
  51. Dispatched before a task is executed.
  52. Sender is the task class being executed.
  53. Provides arguments:
  54. * task_id
  55. Id of the task to be executed.
  56. * task
  57. The task being executed.
  58. * args
  59. the tasks positional arguments.
  60. * kwargs
  61. The tasks keyword arguments.
  62. .. data:: task_postrun
  63. Dispatched after a task has been executed.
  64. Sender is the task class executed.
  65. Provides arguments:
  66. * task_id
  67. Id of the task to be executed.
  68. * task
  69. The task being executed.
  70. * args
  71. The tasks positional arguments.
  72. * kwargs
  73. The tasks keyword arguments.
  74. * retval
  75. The return value of the task.
  76. Worker Signals
  77. --------------
  78. .. data:: worker_init
  79. Dispatched before the worker is started.
  80. .. data:: worker_ready
  81. Dispatched when the worker is ready to accept work.
  82. .. data:: worker_process_init
  83. Dispatched by each new pool worker process when it starts.
  84. .. data:: worker_shutdown
  85. Dispatched when the worker is about to shut down.