A distributed lock implementation based on SQLAlchemy
Project description
SQLAlchemy-DLock
Distributed lock based on Database and SQLAlchemy.
It currently supports locks of:
- MySQL: https://dev.mysql.com/doc/refman/8.0/en/locking-functions.html
- PostgreSQL: https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS
It's not stable and DO NOT use it in production.
Usages
Basic:
from sqlalchemy import create_engine
from sqlalchemy_dlock import make_session_level_lock as sd_lock
# ...
lock_key = 'user/001'
# ...
engine = create_engine()
# ...
with engine.connect() as conn:
with sd_lock(conn, lock_key):
# do sth...
pass
# ...
Made from SQLAlchemy's Session:
# ...
lock_key = 'user/001'
# ...
with Session.bind.connect() as conn:
with sd_lock(conn, lock_key):
# ...
user = Session.query(User).filter(id='001').one()
user.password = new_pass
Session.commit()
# ...
# ...
Or, if the session
has no commit
, rollback
, close
:
# ...
lock_key = 'user/001'
# ...
with sd_lock(Session.connection(), lock_key):
# ...
user = Session.query(User).filter(id='001').one()
password = user.password
# ...
# ...
CHANGELOG
v0.1
An early version, maybe not stable enough.
AUTHORS
-
Liu Xue Yan (liu_xue_yan@foxmail.com)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
SQLAlchemy-DLock-0.1.tar.gz
(20.4 kB
view hashes)
Built Distribution
Close
Hashes for SQLAlchemy_DLock-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e93d422e81731a9cf06b2a5aa7abe1c6cb564f48ba34b756a2537ad5794c2430 |
|
MD5 | 30991e9fd2a0835113590538f1cd03c6 |
|
BLAKE2b-256 | c2ddffbe2f82223b2c8e1b2a92b3ede7e662caa40ae6599d6249095bb467fb63 |