sqlalchemy_dlock.lock.mysql module

class sqlalchemy_dlock.lock.mysql.MysqlAsyncSadLock(connection_or_session, key, **kwargs)[source]

Bases: MysqlSadLockMixin, BaseAsyncSadLock[str, AsyncConnection | AsyncSession | async_scoped_session]

Async IO version of MysqlSadLock

Parameters:
  • key (Any) –

    MySQL named lock requires the key given by string.

    If key is not a str:

    • When bytes or alike, the constructor tries to decode it with default encoding:

      key = key.decode()
      
    • Otherwise the constructor force convert it to str:

      key = str(key)
      
    • Or you can specify a convert function to that argument

  • convert

    Custom function to covert key to required data type.

    Example

    def convert(value) -> str:
        # get a string key by `value`
        return the_string_covert_from_value
    

  • connection_or_session (AsyncConnection | AsyncSession | async_scoped_session)

async do_acquire(block=True, timeout=None, *args, **kwargs)[source]
Return type:

bool

Parameters:
async do_release()[source]
class sqlalchemy_dlock.lock.mysql.MysqlSadLock(connection_or_session, key, **kwargs)[source]

Bases: MysqlSadLockMixin, BaseSadLock[str, Connection | Session | scoped_session]

A distributed lock implemented by MySQL named-lock

Caution

To MySQL locking function, it is even possible for a given session to acquire multiple locks for the same name. Other sessions cannot acquire a lock with that name until the acquiring session releases all its locks for the name. When perform multiple acquire() for a key on the same SQLAlchemy connection, latter acquire() will success immediately no wait and never block, it causes cascade lock instead!

Parameters:
do_acquire(block=True, timeout=None, *args, **kwargs)[source]
Return type:

bool

Parameters:
do_release()[source]
class sqlalchemy_dlock.lock.mysql.MysqlSadLockMixin(*, key, convert=None, **kwargs)[source]

Bases: AbstractLockMixin[KTV, str]

A Mix-in class for MySQL named lock

Parameters:
  • key (TypeVar(KTV, bound= Any)) –

    MySQL named lock requires the key given by string.

    If key is not a str:

    • When bytes or alike, the constructor tries to decode it with default encoding:

      key = key.decode()
      
    • Otherwise the constructor force convert it to str:

      key = str(key)
      
    • Or you can specify a convert function to that argument

  • convert (Callable[[TypeVar(KTV, bound= Any)], str] | None) –

    Custom function to covert key to required data type.

    Example

    def convert(value) -> str:
        # get a string key by `value`
        return the_string_covert_from_value
    

classmethod convert(k)[source]

The default key converter for MySQL named lock

Return type:

str

Parameters:

k (bytes | bytearray | memoryview | str | int | float)

get_actual_key()[source]

The actual key used in MySQL named lock

Return type:

str