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](https://py-filelock.readthedocs.io).

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

size()#

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

Return type

int

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.

Parameters
  • 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()

all_items()#

Return a list as copies of all items in the bucket

Return type

List[float]

close()#

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

Connection

flush()#

Flush/reset bucket

get(number=1)#

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

Return type

int

lock_acquire()#

Acquire a lock prior to beginning a new transaction

lock_release()#

Release lock following a transaction

put(item)#

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

Return type

int

size()#

Keep bucket size in memory to avoid some unnecessary reads

Return type

int