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.1
- An early version, maybe not stable enough.
- Replace black2b with crc64-iso in PostgreSQL key convert function
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.1.tar.gz
(21.4 kB
view hashes)
Built Distribution
Close
Hashes for SQLAlchemy_DLock-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cb124f2df1721ed12bf8cc388aec239b285ea46d928541f4a00b5b025ab45a8 |
|
MD5 | c1c343213d91f30dac9f0761cd6934cf |
|
BLAKE2b-256 | 8380b39820410c26dbd2198bd5a0a84bd5855659054b4c916ee9274b7a0ead0b |