signals.py 3.6 KB

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