Skip to main content

Django celery token bucket

A dynamic token bucket implementation using the database scheduler django celery beat.

How it works

The bucket is represented by a celery queue that will not be processed by a worker but just hold our tokens (messages). Whenever a rate limited task should be run, the decorator tries to consume a message from that queue. If the queue is empty, the task gets retried after the defined timeout.
A periodic task will then refill the bucket with tokens whenever they should be available again.

Define a token bucket

Buckets are defined in the Django config.

Following example allows one thousand tokens per hour to throttle access to a rate limited third party API.

Add to settings.py of your project.

from typing import Dict
from celery import schedules
from django_celery_token_bucket.bucket import TokenBucket

INSTALLED_APPS = [
    ...,
    'django_celery_token_bucket'
]

CELERY_TOKEN_BUCKETS: Dict[str, TokenBucket] = {
    "my_api_client": TokenBucket(
        name="my_api_client",
        schedule=schedules.crontab(minute=0),  # once every hour
        amount=1000,
        maximum=1000,
    )
}

name

The name must only consist of letters, numbers and the underscore character as it's used in the name of the celery queue. It should also be the same as the key in the CELERY_TOKEN_BUCKETS dictionary.

schedule

A celery.schedules.crontab that defines when the tokens should be refilled.

amount

The amount of tokens to add whenever the scheduled refill is run.

maximum

The maximum amount of tokens our bucket can hold.

token_refill_queue

Override this setting if you want token refill tasks for this bucket to be placed on a specific queue. This field is by default None. If no value is provided, the CELERY_DEFAULT_QUEUE setting is used or celery

Sync period tasks to refill the buckets

A management command token_bucket_register_periodic_tasks is provided that should be run during deployment of your application to sync the period tasks and make sure that buckets get properly refilled.

Use the rate_limit decorator

The decorator will make sure that the task that gets decorated will not exceed the limit of available tokens.
Decorated tasks must always be bound to allow access to the task instance.

from my_app.celery import celery_app
from django_celery_token_bucket.decorators import rate_limit


@celery_app.task(bind=True)
@rate_limit(token_bucket_name="my_api_client", countdown=300)
def my_tasK(self, *args, **kwargs):
    return

token_bucket

Name of the token bucket to consume from. Must be defined in the settings (see above) or will fail with an Exception.

countdown

Time to wait in seconds before the next try when no token is available.

affect_task_retries

Defaults to False
By default a failed token retrieval will not impact the retry count of your task. To change this behavior, set affect_task_retries to True.

@celery_app.task(bind=True, max_retries=3, countdown=60)
@rate_limit(token_bucket="my_api_client", countdown=300, affect_task_retries=True)
def my_tasK(self, *args, **kwargs):
    return

In this scenario, a failed token retrieval will increase the retry count of the task decorator. If we cannot get a token on the first try, we will start over again with the 2nd try.

Run the tests locally

A docker compose environment is provided to easily run the tests:

docker compose run --rm app test

Making a new release

This project makes use of RegioHelden's reusable GitHub workflows.
Make a new release by manually triggering the Open release PR workflow.

Metadata

Release files for django-celery-token-bucket 5.0.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 django-celery-token-bucket 5.0.0
File Size Uploaded Provenance
django_celery_token_bucket-5.0.0.tar.gz 9.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-celery-token-bucket 5.0.0
File Interpreter ABI Platform
django_celery_token_bucket-5.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.3 kB

Release files / django_celery_token_bucket-5.0.0.tar.gz

Download URL django_celery_token_bucket-5.0.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a836f111223b245d852eb5b8fee1b43ba002957f6bddeff2e2f3d8ae201be79c
BLAKE2b-256 checksum
How to use checksums
0790904b98c81a5c23d98a5f631a0a4de2120b13553b5a1b68acc37c8ac1edc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / django_celery_token_bucket-5.0.0-py3-none-any.whl

Download URL django_celery_token_bucket-5.0.0-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2351085f63c382a3d42d3d1acbd77f651ca6a6c31f272d1c1343c4d55ed7422d
BLAKE2b-256 checksum
How to use checksums
2fc8a6d9d467e02458d7ef007d03de8a779ae844ae98ac72b6c247e9a72dfa90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for django-celery-token-bucket 5.0.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.

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