Skip to main content

BaseLoader

Base class for loaders.

Loaders handles,

* Reading celery client/worker configurations.

* What happens when a task starts?
See :meth:`on_task_init`.

* What happens when the worker starts?
See :meth:`on_worker_init`.

* What happens when the worker shuts down?
See :meth:`on_worker_shutdown`.

* What modules are imported to find tasks?

Attributes

AttributeTypeDescription
builtin_modulesfrozenset = frozenset()A set of module names that are automatically imported by the loader to find tasks.
configuredboolean = falseA boolean flag indicating whether the loader has successfully read and applied its configuration.
override_backendsdict = {}A mapping used to specify alternative result backend classes that override the default configuration.
worker_initializedboolean = falseA boolean flag that tracks if the worker-specific initialization process has been completed.

Constructor

Signature

def BaseLoader(
app: Any,
**kwargs: dict
)

Parameters

NameTypeDescription
appAnyThe Celery application instance associated with this loader.
**kwargsdictAdditional keyword arguments.

Methods


now()

@classmethod
def now(
utc: boolean = True
) - > datetime

Returns the current date and time, optionally in UTC format.

Parameters

NameTypeDescription
utcboolean = TrueDetermines whether to return the time in UTC or local time

Returns

TypeDescription
datetimeThe current timestamp as a datetime object

on_task_init()

@classmethod
def on_task_init(
task_id: string,
task: [Task](../../events/state/task.md?sid=celery_events_state_task)
)

Called before a task is executed.

Parameters

NameTypeDescription
task_idstringThe unique identifier of the task being initialized
task[Task](../../events/state/task.md?sid=celery_events_state_task)The task instance that is about to be executed

on_process_cleanup()

@classmethod
def on_process_cleanup()

Called after a task is executed.


on_worker_init()

@classmethod
def on_worker_init()

Called when the worker (:program:celery worker) starts.


on_worker_shutdown()

@classmethod
def on_worker_shutdown()

Called when the worker (:program:celery worker) shuts down.


on_worker_process_init()

@classmethod
def on_worker_process_init()

Called when a child process starts.


import_task_module()

@classmethod
def import_task_module(
module: string
) - > module

Imports a specific task module and tracks it within the loader's task module set.

Parameters

NameTypeDescription
modulestringThe name of the module to import

Returns

TypeDescription
moduleThe imported module object

import_module()

@classmethod
def import_module(
module: string,
package: string = null
) - > module

Imports a module by name using the standard importlib mechanism.

Parameters

NameTypeDescription
modulestringThe name of the module to import
packagestring = nullThe package name to use for relative imports

Returns

TypeDescription
moduleThe imported module object

import_from_cwd()

@classmethod
def import_from_cwd(
module: string,
imp: callable = null,
package: string = null
) - > module

Imports a module while ensuring the current working directory is included in the search path.

Parameters

NameTypeDescription
modulestringThe name of the module to import
impcallable = nullThe underlying import function to use; defaults to self.import_module
packagestring = nullThe package name for relative imports

Returns

TypeDescription
moduleThe imported module object

import_default_modules()

@classmethod
def import_default_modules() - > list

Imports all modules defined in the default_modules property and triggers the import_modules signal.

Returns

TypeDescription
listA list of the imported module objects

init_worker()

@classmethod
def init_worker()

Initializes the worker state by importing default modules and triggering the worker initialization hook.


shutdown_worker()

@classmethod
def shutdown_worker()

Triggers the worker shutdown hook to perform cleanup tasks.


init_worker_process()

@classmethod
def init_worker_process()

Triggers the child process initialization hook.


config_from_object()

@classmethod
def config_from_object(
obj: object|string,
silent: boolean = False
) - > boolean

Loads configuration values from a given object, dictionary, or module path.

Parameters

NameTypeDescription
obj`objectstring`
silentboolean = FalseIf True, suppresses ImportErrors when the object is a string path

Returns

TypeDescription
booleanTrue if the configuration was successfully loaded

find_module()

@classmethod
def find_module(
module: string
) - > object

Locates a module within the filesystem or python path.

Parameters

NameTypeDescription
modulestringThe name of the module to find

Returns

TypeDescription
objectThe module location information

cmdline_config_parser()

@classmethod
def cmdline_config_parser(
args: list,
namespace: string = 'celery',
re_type: regex = re.compile(r'\((\w+)\)'),
extra_types: dict = null,
override_types: dict = null
) - > dict

Parses command-line configuration arguments into a dictionary of settings.

Parameters

NameTypeDescription
argslistA list of 'key=value' strings from the command line
namespacestring = 'celery'The default prefix for configuration keys
re_typeregex = re.compile(r'((\w+))')Regex used to detect type casting hints in values
extra_typesdict = nullAdditional type mapping functions for custom casting
override_typesdict = nullMappings to override default type behaviors

Returns

TypeDescription
dictA dictionary of parsed configuration keys and their cast values

read_configuration()

@classmethod
def read_configuration(
env: string = 'CELERY_CONFIG_MODULE'
) - > DictAttribute|null

Reads the configuration module specified in the environment variables.

Parameters

NameTypeDescription
envstring = 'CELERY_CONFIG_MODULE'The environment variable name that holds the config module path

Returns

TypeDescription
`DictAttributenull`

autodiscover_tasks()

@classmethod
def autodiscover_tasks(
packages: list,
related_name: string = 'tasks'
)

Searches for task modules within specified packages and adds them to the internal registry.

Parameters

NameTypeDescription
packageslistA list of package names to search for tasks
related_namestring = 'tasks'The name of the module to look for within each package

default_modules()

@classmethod
def default_modules() - > tuple

Fetches the list of modules to be imported by default, including built-ins and app-configured imports.

Returns

TypeDescription
tupleA tuple of module names to import

conf()

@classmethod
def conf() - > dict

Loader configuration.

Returns

TypeDescription
dictThe current configuration mapping