pytokenbucket package

Submodules

pytokenbucket.tokenbucket module

Implementation of a thread-safe token bucket using threads and queues.

class pytokenbucket.tokenbucket.TokenBucket(bucket_size=10, refresh_amount=1, refresh_period_ms=1000, start_filled=True)[source]

Bases: object

A token bucket. Tokens can be requested using get_token.

Construct a TokenBucket instance.

Parameters:
  • bucket_size (int) – The maximum number of tokens this bucket can hold. Extra tokens are thrown away.
  • refresh_amount (int) – The number of tokens to refresh each period.
  • refresh_period_ms (int) – The duration between token refreshes in milliseconds.
  • start_filled (bool) – Whether the bucket starts filled or empty.
deferred_call(callable)[source]

Return a callable which calls the argument when a token is available.

This method can be used to make a wrapper for a callable which can then be used by the multiprocessing.dummy Pool with the apply_async method. It also abstracts away the internals of getting a token.

Parameters:callable (Callable) – a callable which is called when a token is available. If the token bucket is stopped before a token is available, the callable is not called.
Returns:A callable which waits for a token to become available before calling the inner callable.
get_token()[source]

Get a token. Blocks until a token is retrieved or the token bucket is stopped.

Returns:True if a token was retrieved. False if the token bucket was stopped.
stop()[source]

Stop the token bucket pending a shutdown.

Module contents

Top-level package for Python Token Bucket.

class pytokenbucket.TokenBucket(bucket_size=10, refresh_amount=1, refresh_period_ms=1000, start_filled=True)[source]

Bases: object

A token bucket. Tokens can be requested using get_token.

Construct a TokenBucket instance.

Parameters:
  • bucket_size (int) – The maximum number of tokens this bucket can hold. Extra tokens are thrown away.
  • refresh_amount (int) – The number of tokens to refresh each period.
  • refresh_period_ms (int) – The duration between token refreshes in milliseconds.
  • start_filled (bool) – Whether the bucket starts filled or empty.
deferred_call(callable)[source]

Return a callable which calls the argument when a token is available.

This method can be used to make a wrapper for a callable which can then be used by the multiprocessing.dummy Pool with the apply_async method. It also abstracts away the internals of getting a token.

Parameters:callable (Callable) – a callable which is called when a token is available. If the token bucket is stopped before a token is available, the callable is not called.
Returns:A callable which waits for a token to become available before calling the inner callable.
get_token()[source]

Get a token. Blocks until a token is retrieved or the token bucket is stopped.

Returns:True if a token was retrieved. False if the token bucket was stopped.
stop()[source]

Stop the token bucket pending a shutdown.