betty.concurrent module

Provide utilities for concurrent programming.

final class betty.concurrent.AsynchronizedLock[source]

Bases: Lock

Make a sychronous (blocking) lock asynchronous (non-blocking).

__init__(lock: lock)[source]
async acquire(*, wait: bool = True) bool[source]

Acquire the lock.

async release() None[source]

Release the lock.

classmethod threading() Self[source]

Create a new thread-safe, asynchronous lock.

class betty.concurrent.Ledger[source]

Bases: object

Lazily create locks by keeping a ledger.

The ledger lock is released once a transaction lock is acquired.

__init__(ledger_lock: Lock)[source]
ledger(transaction_id: Hashable) Lock[source]

Ledger a new lock for the given transaction ID.

class betty.concurrent.Lock[source]

Bases: ABC

Provide an asynchronous lock.

abstractmethod async acquire(*, wait: bool = True) bool[source]

Acquire the lock.

abstractmethod async release() None[source]

Release the lock.

final class betty.concurrent.RateLimiter[source]

Bases: object

Rate-limit operations.

This class implements the Token Bucket algorithm.

This is thread-safe, which means you can safely use this between different threads.

__init__(maximum: int)[source]
async wait() None[source]

Wait until an operation may be performed (again).

async betty.concurrent.asynchronize_acquire(lock: lock, *, wait: bool = True) bool[source]

Acquire a synchronous lock asynchronously.