pyrate_limiter.sqlite_bucket module#

class pyrate_limiter.sqlite_bucket.FileLockSQLiteBucket(**kwargs)#

Bases: pyrate_limiter.sqlite_bucket.SQLiteBucket

Bucket backed by a SQLite database and file lock. Suitable for usage from multiple processes with no shared state. Requires installing [py-filelock](

The file lock is reentrant and shared across buckets, allowing a process to access multiple buckets at once.


Query current size from the database for each call instead of keeping in memory

Return type


class pyrate_limiter.sqlite_bucket.SQLiteBucket(maxsize=0, identity=None, path=PosixPath('/tmp/pyrate_limiter.sqlite'), **kwargs)#

Bases: pyrate_limiter.bucket.AbstractBucket

Bucket backed by a SQLite database. Will be stored in the system temp directory by default.

Notes on concurrency:

  • Thread-safe

  • Safe for use with multiple child processes with a shared initial state and using the same Limiter object, e.g. if created with ProcessPoolExecutor or multiprocessing.Process.

  • For other usage with multiple processes, see FileLockSQLiteBucket.

  • Transactions are locked at the bucket level, but not at the connection or database level.

  • The default isolation level is used (autocommit).

  • Multitple buckets may be used in parallel, but a given bucket will only be used by one thread/process at a time.

  • maxsize (int) – Maximum number of items in the bucket

  • identity (Optional[str]) – Bucket identity, used as the table name

  • path (Union[Path, str]) – Path to the SQLite database file; defaults to a temp file in the system temp directory

  • kwargs – Additional keyword arguments for sqlite3.connect()


Return a list as copies of all items in the bucket

Return type



Close the database connection

property connection#

Create a database connection and initialize the table, if it hasn’t already been done. This is safe to leave open, but may be manually closed with close(), if needed.

Return type



Flush/reset bucket


Get items and remove them from the bucket in the FIFO fashion. Return the number of items that have been removed.

Return type



Acquire a lock prior to beginning a new transaction


Release lock following a transaction


Put an item in the bucket. Return 1 if successful, else 0

Return type



Keep bucket size in memory to avoid some unnecessary reads

Return type


pyrate_limiter.sqlite_bucket.chunkify(iterable, max_size)#

Split an iterable into chunks of a max size

Return type