Skip to content

Workers Workers

Workers in Python's pyproject.toml


Documentation: https://workers.software

Source Code: tiangolo/workers


Description

Workers lets you define your project's background programs in one place: the pyproject.toml file.

These programs are called workers. They keep running to do work, like sending emails or processing files.

A provider is a service or tool that runs your application. Providers can support this format to read your configuration and start your workers.

Example

Each worker needs a name and a command to run.

Add this to your pyproject.toml:

[tool.workers.my-background-worker]
command = "python -m app.worker"

Here, my-background-worker is the worker's name. The command runs the Python module app.worker, in this case the file app/worker.py.

That file could contain:

from time import sleep

while True:
    print("Doing background work", flush=True)
    sleep(1)

A provider that supports Workers would run this command in a separate process, a running copy of your program.

This example prints a message every second until the worker is stopped.

Use Cases

You can use workers with Celery, RQ, or Sheppy. These libraries let workers take tasks from a queue, where tasks wait until a worker can run them.

For example, to start a Celery worker:

[tool.workers.my-tasks-worker]
command = "celery -A app.celery worker"

Here, app.celery is the module where you define your Celery app.

You can also use your own Python code, like the loop in the example above.

Workers Format

Each worker has a name, also called its ID, and one required setting: command.

[tool.workers.<worker-id>]
command = "<command to run>"

Worker ID

Replace <worker-id> with a name you choose, like my-tasks-worker or reports. If you want several workers, give each one a different ID.

Keep tool.workers as written. For example, [tool.workers.some-important-reports] defines the worker named some-important-reports.

command

The command to run for this worker, as a string.

[tool.workers.my-background-worker]
command = "python -m app.worker"

The provider runs the command from the directory containing pyproject.toml.

The command should keep running in the foreground until the worker is stopped.

The provider runs the command using a shell compatible with POSIX sh. This lets you use environment variables set by the provider:

[tool.workers.my-tasks-worker]
command = 'celery -A app.celery worker --concurrency "$WORKER_CONCURRENCY"'

The shell replaces $WORKER_CONCURRENCY with the value of that environment variable. For example, you could set WORKER_CONCURRENCY to 4 in your provider's settings.

With FastAPI

You can configure your FastAPI app and your workers in the same pyproject.toml.

For example, you could have an API with a Celery worker and a Sheppy worker:

[tool.fastapi]
entrypoint = "app.main:app"

[tool.workers.my-background-worker]
command = "celery -A app.celery worker"

[tool.workers.some-important-reports]
command = "sheppy work"

You choose the worker names: my-background-worker and some-important-reports in this example.

The FastAPI entrypoint points to your API app. The provider runs each worker's command in a separate process.

Provider Configuration

The provider controls how many replicas (copies) of each worker to run and when to restart them. It can also set environment variables, for example to provide a database URL.

This format only defines the workers you want to run and the command that starts each one.

License

This project is licensed under the terms of the MIT license.