This document describes an older version of Celery (2.1). For the latest stable version please go here.

Signals - celery.signals

Basics

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

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, ))

task_sent.connect(task_sent_handler)

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")

Signals

Task Signals

celery.signals.task_sent

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:

  • task_id

    Id of the task to be executed.

  • task

    The task being executed.

  • args

    the tasks positional arguments.

  • kwargs

    The tasks keyword arguments.

  • eta

    The time to execute the task.

  • taskset

    Id of the taskset this task is part of (if any).

celery.signals.task_prerun

Dispatched before a task is executed.

Sender is the task class being executed.

Provides arguments:

  • task_id

    Id of the task to be executed.

  • task

    The task being executed.

  • args

    the tasks positional arguments.

  • kwargs

    The tasks keyword arguments.

celery.signals.task_postrun

Dispatched after a task has been executed.

Sender is the task class executed.

Provides arguments:

  • task_id

    Id of the task to be executed.

  • task

    The task being executed.

  • args

    The tasks positional arguments.

  • kwargs

    The tasks keyword arguments.

  • retval

    The return value of the task.

Worker Signals

celery.signals.worker_init

Dispatched before the worker is started.

celery.signals.worker_ready

Dispatched when the worker is ready to accept work.

celery.signals.worker_process_init

Dispatched by each new pool worker process when it starts.

celery.signals.worker_shutdown

Dispatched when the worker is about to shut down.

Previous topic

Periodic Task Schedule Behaviors - celery.schedules

Next topic

Exceptions - celery.exceptions

This Page