Skip to main content

DjangoWorkerFixup

This class integrates Celery workers with the Django framework by managing database connections and cache lifecycles across task executions and process initializations. It ensures that Django models are validated and that stale or inherited file descriptors are properly closed to prevent connection leaks in multiprocessing environments. The class also supports configurable database connection reuse limits through signal-based hooks.

Attributes

AttributeTypeDescription
worker[WorkController](../../worker/worker/workcontroller.md?sid=celery_worker_worker_workcontroller) = nullSet via on_worker_init callback to avoid recursive WorkController instantiation

Constructor

Signature

def DjangoWorkerFixup(
app: [Celery](../../app/base/celery.md?sid=celery_app_base_celery)
) - > None

Parameters

NameTypeDescription
app[Celery](../../app/base/celery.md?sid=celery_app_base_celery)The Celery application instance to be used by the fixup.

Methods


django_setup()

@classmethod
def django_setup() - > None

Initializes the Django environment by calling the standard django.setup() routine.

Returns

TypeDescription
None

validate_models()

@classmethod
def validate_models() - > None

Runs Django system checks to ensure model integrity unless the CELERY_SKIP_CHECKS environment variable is set.

Returns

TypeDescription
None

install()

@classmethod
def install() - > [DjangoWorkerFixup](djangoworkerfixup.md?sid=celery_fixups_django_djangoworkerfixup)

Hooks the fixup into Celery signals to manage database and cache connections throughout the worker lifecycle.

Returns

TypeDescription
[DjangoWorkerFixup](djangoworkerfixup.md?sid=celery_fixups_django_djangoworkerfixup)The current instance of the fixup class for chaining.

on_worker_process_init()

@classmethod
def on_worker_process_init(
kwargs: Any
) - > None

Handles initialization for child worker processes, including re-validating models and safely clearing inherited file descriptors.

Parameters

NameTypeDescription
kwargsAnyArbitrary keyword arguments passed by the worker_process_init signal.

Returns

TypeDescription
None

on_task_prerun()

@classmethod
def on_task_prerun(
sender: [Task](../../events/state/task.md?sid=celery_events_state_task),
kwargs: Any
) - > None

Called before every task.

Parameters

NameTypeDescription
sender[Task](../../events/state/task.md?sid=celery_events_state_task)The Celery task instance that is about to be executed.
kwargsAnyAdditional signal arguments such as task_id and task args/kwargs.

Returns

TypeDescription
None

on_task_postrun()

@classmethod
def on_task_postrun(
sender: [Task](../../events/state/task.md?sid=celery_events_state_task),
kwargs: Any
) - > None

Cleans up database and cache connections after a task finishes to prevent connection leakage.

Parameters

NameTypeDescription
sender[Task](../../events/state/task.md?sid=celery_events_state_task)The Celery task instance that has completed execution.
kwargsAnyAdditional signal arguments such as task_id and return value.

Returns

TypeDescription
None

close_database()

@classmethod
def close_database(
kwargs: Any
) - > None

Closes database connections based on the CELERY_DB_REUSE_MAX setting to manage connection recycling.

Parameters

NameTypeDescription
kwargsAnyGeneric keyword arguments often passed by signal handlers.

Returns

TypeDescription
None

close_cache()

@classmethod
def close_cache() - > None

Closes all active Django cache connections to ensure fresh state for subsequent tasks.

Returns

TypeDescription
None