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
| Attribute | Type | Description |
|---|---|---|
| worker | [WorkController](../../worker/worker/workcontroller.md?sid=celery_worker_worker_workcontroller) = null | Set 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
| Name | Type | Description |
|---|---|---|
| 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
| Type | Description |
|---|---|
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
| Type | Description |
|---|---|
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
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| kwargs | Any | Arbitrary keyword arguments passed by the worker_process_init signal. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| sender | [Task](../../events/state/task.md?sid=celery_events_state_task) | The Celery task instance that is about to be executed. |
| kwargs | Any | Additional signal arguments such as task_id and task args/kwargs. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| sender | [Task](../../events/state/task.md?sid=celery_events_state_task) | The Celery task instance that has completed execution. |
| kwargs | Any | Additional signal arguments such as task_id and return value. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| kwargs | Any | Generic keyword arguments often passed by signal handlers. |
Returns
| Type | Description |
|---|---|
None |
close_cache()
@classmethod
def close_cache() - > None
Closes all active Django cache connections to ensure fresh state for subsequent tasks.
Returns
| Type | Description |
|---|---|
None |