This document describes the current stable version of Kombu (4.5). For development docs, go here.

Rate limiting - kombu.utils.limits

Token bucket implementation for rate limiting.

class kombu.utils.limits.TokenBucket(fill_rate, capacity=1)[source]

Token Bucket Algorithm.

See also

Most of this code was stolen from an entry in the ASPN Python Cookbook:


Thread Safety: This implementation is not thread safe. Access to a TokenBucket instance should occur within the critical section of any multithreaded code.


Check if one or more tokens can be consumed.

true if the number of tokens can be consumed
from the bucket. If they can be consumed, a call will also consume the requested number of tokens from the bucket. Calls will only consume tokens (the number requested) or zero tokens – it will never consume a partial number of tokens.
Return type:bool
capacity = 1

Maximum number of tokens in the bucket.


Return estimated time of token availability.

Returns:the time in seconds.
Return type:float
fill_rate = None

The rate in tokens/second that the bucket will be refilled.

timestamp = None

Timestamp of the last time a token was taken out of the bucket.