celery.signals.rst 2.7 KB

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