Skip to main content

Continuous Integration Coverage Report

Easy Configuration For Celery App Using a Yaml File

celery-yaml is a library to inject a --yaml option to the celery worker command in order to inject its configuration.

It also handle help to configurate this application for Pyramid application.

Usage

celery -A my_application.module_containing_my.app worker --yaml development.yaml ...

This will configure the application my_application containing an application app in a submodule module_containing_my.

The celery app must register the --yaml using the add_yaml_option on the app instance, this way:

from celery import Celery
from celery_yaml import add_yaml_option

app = Celery()
add_yaml_option(app)

Yaml format

celery:
  broker_url: "amqp://guest:guest@localhost:5672//"
  result_backend: "rpc://"
  imports:
    - my_application.tasks
  # see all settings in the celery docs:
  # https://docs.celeryproject.org/en/stable/userguide/configuration.html

logging:
  version: 1
  # dictConfig format
  # https://docs.python.org/3/library/logging.config.html#logging-config-dictschema

NOTE

To avoid password in the configuration file:

  • Every variables can be substitute with an environment variable. The yaml file will replace variable using envsub.

    celery:
      broker_url: "${CELERY_BROKER_URL-amqp://guest:guest@localhost:5672//}"
    
  • DEPRECATED: The broker_url is always overriden by the environment variable CELERY_BROKER_URL without any declaration.


Using celery beat

Celery beat can schedule tasks using interval or crontab.

The schedule parameter can be an int, a float or a mapping. If it is a mapping, it will be cast to a crontab, the mapping will be passed as acrontab parameters.

celery:
  beat_schedule:
    beat_second:
      task: beat_it
      schedule: 10 # example of interval schedule, every 10 seconds

    beat_crontab:
      task: beat_it
      schedule: # example of crontab
        hour: 9
        minute: 0
        day_of_week: 1

Using Celery in a Pyramid App.

The extras "pyramid" must be added to install the extras depencencies.

With PEP 621 (uv, pdm, ...)

[dependency-groups]
celery = ["celery >=5.2.7,<6", "celery-yaml[pyramid]>=2.0.0,<3"]

[project.entry-points."paste.app_factory"]
main = "pyramid_helloworld:main"

[project.entry-points."celery_yaml.app"]
main = "pyramid_helloworld.backend:app"

With poetry

[tool.poetry.dependencies]
celery-yaml = { version = "^2.0.0", extras = ["pyramid"] }

[tool.poetry.plugins."paste.app_factory"]
main = "pyramid_app.wsgi:main"

[tool.poetry.plugins."celery_yaml.app"]
main = "pyramid_app.backend:app"

[tool.poetry.plugins."plaster.loader_factory"]
"file+yaml" = "plaster_yaml:Loader"

the paste.app_factory is used by Pyramid itself to build the WSGI sergivice but we add a plaster.loader_factory to configure the usage of a yaml file instead of an ini file to configure it.

Then the celery_yaml.app is used by celery-yaml as an entrypoint to the celery app.

Then, in the configuration file,

celery: &celery
  result_backend: "rpc://"
  imports:
    - pyramid_app.tasks

app:
  "use": "egg:pyramid_app"
  "pyramid.includes": ["celery_yaml"]
  "celery":
    <<: *celery
    "use": "egg:pyramid_app"

foo: "bar"

More configuration

if the celery app as a method on_yaml_loaded then the function is called with the data and the filepath in parameter. It may be used to get mode configuration available readed from the yaml file.

Example

from celery import Celery as CeleryBase


class Celery(CeleryBase):

    def on_yaml_loaded(self, data: dict[str, Any], config_path: str):
        data['foo']  # you can access to the value of foo here

This is particullary usefull for depenency injection purpose.

Environment substitution

celery:
  broker_url: ${CELERY_BROKER_URL}
  result_backend: ${CELERY_BACKEND_RESULT}
  imports:
    - pyramid_app.tasks

database_dsn: ${DATABASE_DSN}

The CELERY_BROKER_URL and CELERY_BACKEND_RESULT environment variable are interpreted by celery, so can be outside your yaml file, but, it is not True for all the variable, using celery-yaml, you can still inject configuration in the yaml that are substituted in the configuration during the yaml load.

This is particullary usefull to keep your configuration structured but keep secrets in environment variable, for security reason. And also to build docker container that can be easily customizable.

See full example in the examples directory:

https://github.com/mardiros/celery-yaml/tree/master/examples/pyramid-app

Metadata

Release files for celery-yaml 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for celery-yaml 2.3.0
File Size Uploaded Provenance Actions
celery_yaml-2.3.0.tar.gz 6.7 kB Without provenance Details for celery_yaml-2.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for celery-yaml 2.3.0
File Interpreter ABI Platform Actions
celery_yaml-2.3.0-py3-none-any.whl Python 3 none any Details for celery_yaml-2.3.0-py3-none-any.whl

Total release size: 14.5 kB

Release files / celery_yaml-2.3.0.tar.gz

Download URL celery_yaml-2.3.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
34ba8ce0aaeb30596d9669e15973e29d43829bf1e475e6bb424001081963b68b
BLAKE2b-256 checksum
How to use checksums
2534c83a6c37bacc1b2e7b9f07bdab012ddc2254e8517565123722d897e7e449
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.29.7 CPython/3.12.16 Linux/7.0.0-1017-azure

Release files / celery_yaml-2.3.0-py3-none-any.whl

Download URL celery_yaml-2.3.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
077bed3fa7e1e115948b1af4870ac9472b2fd167be939e49fb336371b5437e94
BLAKE2b-256 checksum
How to use checksums
5ec46f47e690fca1967528f4a693cb9ab0c73abac6a9fd96d255a0126a7c0b9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.29.7 CPython/3.12.16 Linux/7.0.0-1017-azure

Security for celery-yaml 2.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page