SQLAlchemy-DLock

https://readthedocs.org/projects/sqlalchemy-dlock/badge/?version=latestDocumentation Status

Distributed lock based on Database and SQLAlchemy.

It currently supports locks of:

It’s not stable and DO NOT use it in production.

Usages

Basic usage:

# ...
from sqlalchemy import create_engine
from sqlalchemy_dlock import sadlock

# ...

lock_key = 'user/001'

# ...

engine = create_engine('postgresql://scott:tiger@localhost/')

# ...

with engine.connect() as conn:
    with sadlock(conn, lock_key):
        pass
        # locked here!
        # ...
    pass
    # unlocked here!
    # ...
# ...

Work with SQLAlchemy’s Session:

# ...
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_dlock import sadlock

# ...

lock_key = 'user/001'

# ...

engine = create_engine('postgresql://scott:tiger@localhost/')
Session = sessionmaker(bind=engine)

# ...

session = Session()

# ...

with session.bind.connect() as conn:
    with sadlock(conn, lock_key):
        # locked here!
        # ...
        user = session.query('User').filter(id='001').one()
        user.password = 'new password'
        session.commit()
        # ...
    pass
    # unlocked here!
    # ...
# ...