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
| Attribute | Type | Description |
|---|
| builtin_modules | frozenset = frozenset() | A set of module names that are automatically imported by the loader to find tasks. |
| configured | boolean = false | A boolean flag indicating whether the loader has successfully read and applied its configuration. |
| override_backends | dict = {} | A mapping used to specify alternative result backend classes that override the default configuration. |
| worker_initialized | boolean = false | A boolean flag that tracks if the worker-specific initialization process has been completed. |
Constructor
Signature
def BaseLoader(
app: Any,
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|
| app | Any | The Celery application instance associated with this loader. |
| **kwargs | dict | Additional keyword arguments. |
Methods
now()
@classmethod
def now(
utc: boolean = True
) - > datetime
Returns the current date and time, optionally in UTC format.
Parameters
| Name | Type | Description |
|---|
| utc | boolean = True | Determines whether to return the time in UTC or local time |
Returns
| Type | Description |
|---|
datetime | The 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
| Name | Type | Description |
|---|
| task_id | string | The 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
| Name | Type | Description |
|---|
| module | string | The name of the module to import |
Returns
| Type | Description |
|---|
module | The 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
| Name | Type | Description |
|---|
| module | string | The name of the module to import |
| package | string = null | The package name to use for relative imports |
Returns
| Type | Description |
|---|
module | The 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
| Name | Type | Description |
|---|
| module | string | The name of the module to import |
| imp | callable = null | The underlying import function to use; defaults to self.import_module |
| package | string = null | The package name for relative imports |
Returns
| Type | Description |
|---|
module | The 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
| Type | Description |
|---|
list | A 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
| Name | Type | Description |
|---|
| obj | `object | string` |
| silent | boolean = False | If True, suppresses ImportErrors when the object is a string path |
Returns
| Type | Description |
|---|
boolean | True 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
| Name | Type | Description |
|---|
| module | string | The name of the module to find |
Returns
| Type | Description |
|---|
object | The 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
| Name | Type | Description |
|---|
| args | list | A list of 'key=value' strings from the command line |
| namespace | string = 'celery' | The default prefix for configuration keys |
| re_type | regex = re.compile(r'((\w+))') | Regex used to detect type casting hints in values |
| extra_types | dict = null | Additional type mapping functions for custom casting |
| override_types | dict = null | Mappings to override default type behaviors |
Returns
| Type | Description |
|---|
dict | A 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
| Name | Type | Description |
|---|
| env | string = 'CELERY_CONFIG_MODULE' | The environment variable name that holds the config module path |
Returns
| Type | Description |
|---|
| `DictAttribute | null` |
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
| Name | Type | Description |
|---|
| packages | list | A list of package names to search for tasks |
| related_name | string = '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
| Type | Description |
|---|
tuple | A tuple of module names to import |
conf()
@classmethod
def conf() - > dict
Loader configuration.
Returns
| Type | Description |
|---|
dict | The current configuration mapping |