Django prodserver

CI Status Documentation Status Test coverage percentage

uv Ruff pre-commit

PyPI Version Supported Python versions License


Documentation: https://django-prodserver.readthedocs.io

Source Code: https://github.com/nanorepublica/django-prodserver


A management command to start production servers/workers with a consistent interface

Installation

Install this via pip (or your favourite package manager):

pip install django-prodserver

Add the app to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_prodserver",
]

Configuration

Add the PRODUCTION_PROCESSES setting to your settings.py. Below shows an example with a web process and worker process defined.

The comments show other available backend processes that are available to use.

PRODUCTION_PROCESSES = {
    "web": {
        "BACKEND": "django_prodserver.backends.servers.gunicorn.GunicornServer",
        "ARGS": {"bind": "0.0.0.0:8111"},
    },
    # "web": {
    #     "BACKEND": "django_prodserver.backends.servers.granian.GranianASGIServer",
    #     "ARGS": {"address": "0.0.0.0", "port": "8000", "workers": "4"},
    # },
    # "web": {
    #     "BACKEND": "django_prodserver.backends.servers.granian.GranianWSGIServer",
    #     "ARGS": {"address": "0.0.0.0", "port": "8000", "workers": "4"},
    # },
    # "web": {
    #     "BACKEND": "django_prodserver.backends.servers.waitress.WaitressServer",
    #     "ARGS": {},
    # },
    # "web": {
    #     "BACKEND": "django_prodserver.backends.servers.uvicorn.UvicornServer",
    #     "ARGS": {},
    # },
    # "web": {
    #     "BACKEND": "django_prodserver.backends.servers.uvicorn.UvicornWSGIServer",
    #     "ARGS": {},
    # },
    "worker": {
        "BACKEND": "django_prodserver.backends.workers.celery.CeleryWorker",
        "APP": "tests.celery.app",
        "ARGS": {},
    },
    # "worker": {
    #     "BACKEND": "django_prodserver.backends.workers.django_tasks.DjangoTasksWorker",
    #     "ARGS": {},
    # },
    # "beat": {
    #     "BACKEND": "django_prodserver.backends.workers.celery.CeleryBeat",
    #     "APP": "tests.celery.app",
    #     "ARGS": {},
    # },
    # "flower": {
    #     "BACKEND": "django_prodserver.backends.servers.flower.CeleryFlower",
    #     "APP": "tests.celery.app",
    #     "ARGS": {"port": "5555", "address": "0.0.0.0"},
    # },
}

Usage

Once the PRODUCTION_PROCESSES setting has been configured you can then start the processes.

Use the server command to start a web/ASGI/WSGI process:

python manage.py server web

Use the worker command to start a background task worker process:

python manage.py worker worker

Both commands read from the same PRODUCTION_PROCESSES setting; server only accepts server backends and worker only accepts worker backends. If you point one at the wrong kind of backend it will tell you which command to use instead. Run either command with --list to see the configured process names.

Deprecated: python manage.py prodserver continues to work as an alias but is deprecated and will be removed in django-prodserver 4.0.0. Use python manage.py server instead.

Creating a new backend.

Creating a backend is fairly simple. Subclass BaseServerBackend (for a web server) or BaseWorkerBackend (for a background task worker) from django_prodserver.backends.base, then implement the start_server method which should call the underlying process in the best possible way for a production setting. You can also optionally override the prep_server_args method to aid with this to provide any default arguments or formatting to the start_server command.

See django_prodserver.backends.servers (production server backends), django_prodserver.backends.workers (worker backends), and django_prodserver.backends.dev (development runserver-style backends) for examples of existing backends for inspiration. Pull Request’s are welcome for additional backends.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Copier

This package was created with Copier and the browniebroke/pypackage-template project template.