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 acquire(block=True, timeout=None, *args, **kwargs)[source]
Return type:

bool

Parameters:
async 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:
acquire(block=True, timeout=None, *args, **kwargs)[source]

Acquire the lock in blocking or non-blocking mode.

The implementation should provide the following behavior: :rtype: bool

  • When block is True (the default), the method blocks until the lock is in an unlocked state, then sets it to locked and returns True.

  • When block is False, the method call is non-blocking. If the lock is currently locked, it returns False; otherwise, it sets the lock to locked state and returns True.

  • When invoked with a positive floating-point value for timeout, it blocks for at most the specified number of seconds until the lock can be acquired.

  • Invocations with a negative timeout value are equivalent to a timeout of zero.

  • When timeout is None (the default), the timeout period is infinite. The timeout parameter has no effect when block is False and is thus ignored.

  • Returns True if the lock has been acquired or False if the timeout period has elapsed.

Parameters:
Return type:

bool

release()[source]

Release the lock.

Since the class is thread-local, this method cannot be called from another thread or process, nor can it be called from another connection. (Although PostgreSQL’s shared advisory lock supports this).

The implementation should provide the following behavior:

  • Reset the lock to unlocked state and return when the lock is currently locked.

  • Allow exactly one of any other threads blocked waiting for the lock to become unlocked to proceed.

  • Raise a ValueError when invoked on an unlocked lock.

  • Not return a value.

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 (Optional[Callable[[TypeVar(KTV, bound= Any)], str]]) –

    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