This document describes an older version of Celery (2.5). For the latest stable version please go here.
Signals allows decoupled applications to receive notifications when certain actions occur elsewhere in the application.
Celery ships with many signals that you application can hook into to augment behavior of certain actions.
Several kinds of events trigger signals, you can connect to these signals to perform actions as they trigger.
Example connecting to the task_sent signal:
from celery.signals import task_sent
@task_sent.connect
def task_sent_handler(sender=None, task_id=None, task=None, args=None,
kwargs=None, \*\*kwds):
print("Got signal task_sent for task id %s" % (task_id, ))
Some signals also have a sender which you can filter by. For example the task_sent signal uses the task name as a sender, so you can connect your handler to be called only when tasks with name “tasks.add” has been sent by providing the sender argument to connect:
@task_sent.connect(task_sent_handler, sender="tasks.add")
def task_sent_handler(sender=None, task_id=None, task=None, args=None,
kwargs=None, \*\*kwds):
print("Got signal task_sent for task id %s" % (task_id, ))
Dispatched when a task has been sent to the broker. Note that this is executed in the client process, the one sending the task, not in the worker.
Sender is the name of the task being sent.
Provides arguments:
Id of the task to be executed.
The task being executed.
the tasks positional arguments.
The tasks keyword arguments.
The time to execute the task.
Id of the taskset this task is part of (if any).
Dispatched before a task is executed.
Sender is the task class being executed.
Provides arguments:
Id of the task to be executed.
The task being executed.
the tasks positional arguments.
The tasks keyword arguments.
Dispatched after a task has been executed.
Sender is the task class executed.
Provides arguments:
Id of the task to be executed.
The task being executed.
The tasks positional arguments.
The tasks keyword arguments.
The return value of the task.
Dispatched when a task fails.
Sender is the task class executed.
Provides arguments:
Id of the task.
Exception instance raised.
Positional arguments the task was called with.
Keyword arguments the task was called with.
Stack trace object.
The celery.datastructures.ExceptionInfo instance.
This is the first signal sent when celeryd starts up. The sender is the host name of the worker, so this signal can be used to setup worker specific configuration:
from celery.signals import celeryd_init
@celeryd_init.connect(sender="worker12.example.com")
def configure_worker12(conf=None, **kwargs):
conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
or to set up configuration for multiple workers you can omit specifying a sender when you connect:
from celery.signals import celeryd_init
@celeryd_init.connect
def configure_workers(sender=None, conf=None, **kwargs):
if sender in ("worker1.example.com", "worker2.example.com"):
conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
if sender == "worker3.example.com":
conf.CELERYD_PREFETCH_MULTIPLIER = 0
Provides arguments:
This is the celery.apps.worker.Worker instance to be initialized. Note that only the app and hostname attributes have been set so far, and the rest of __init__ has not been executed.
The configuration of the current app.
Dispatched before the worker is started.
Dispatched when the worker is ready to accept work.
Dispatched by each new pool worker process when it starts.
Dispatched when the worker is about to shut down.
Dispatched when celerybeat starts (either standalone or embedded). Sender is the celery.beat.Service instance.
Dispatched in addition to the beat_init signal when celerybeat is started as an embedded process. Sender is the celery.beat.Service instance.
Sent when the eventlet pool has been started.
Sender is the celery.concurrency.eventlet.TaskPool instance.
Sent when the worker shutdown, just before the eventlet pool is requested to wait for remaining workers.
Sender is the celery.concurrency.eventlet.TaskPool instance.
Sent when the pool has been joined and the worker is ready to shutdown.
Sender is the celery.concurrency.eventlet.TaskPool instance.
Sent whenever a task is applied to the pool.
Sender is the celery.concurrency.eventlet.TaskPool instance.
Provides arguments:
target
The target function.
args
Positional arguments.
kwargs
Keyword arguments.
Celery won’t configure the loggers if this signal is connected, so you can use this to completely override the logging configuration with your own.
If you would like to augment the logging configuration setup by Celery then you can use the after_setup_logger and after_setup_task_logger signals.
Provides arguments:
The level of the logging object.
The name of the logfile.
The log format string.
Specify if log messages are colored or not.
Sent after the setup of every global logger (not task loggers). Used to augment logging configuration.
Provides arguments:
The logger object.
The level of the logging object.
The name of the logfile.
The log format string.
Specify if log messages are colored or not.
Sent after the setup of every single task logger. Used to augment logging configuration.
Provides arguments:
The logger object.
The level of the logging object.
The name of the logfile.
The log format string.
Specify if log messages are colored or not.