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.htmlPostgreSQL
: https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS
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!
# ...
# ...
CHANGELOG
0.1.3
Date: 2021-11-09
-
New:
- Better supportings for SQLAlchemy 1.4+, and Scoped Session.
- New
level
argument for PostgreSQL lock.
-
Modify:
- Remove
libscrc
requirement. Now usehashlib.blake2b
to calculate INT64 key for PostgreSQL advisory lock.
- Remove
v0.1.2
Date: 2021-01-26
Still an early version, not for production.
- Changes:
- Arguments and it's default value of
acquire
now similar to stdlib'smultiprossing.Lock
, instead ofThreading.Lock
- MySQL lock now accepts float-point value as
timeout
- Arguments and it's default value of
- Adds
- Several new test cases
- Other
- Many other small adjustment
v0.1.1
- A very early version, maybe not stable enough.
- Replace black2b with crc64-iso in PostgreSQL key convert function
- Only named arguments as extra parameters allowed in Lock's implementation class
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
Built Distribution
Close
Hashes for SQLAlchemy-DLock-0.1.3.post1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d201fec608458dbaff149a2bc96e3ee5bf58ea39fd1251ed385670d42be96b7 |
|
MD5 | 8ded022d738a32a7cb19a512ab062efd |
|
BLAKE2b-256 | 841f2ba19415a98c9c77f062dfd174c1ace6f505b354a223910e196719f29dc3 |
Close
Hashes for SQLAlchemy_DLock-0.1.3.post1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 312b6454c37d722a9449524e8381edd22f6bb57aea9ef12321577a786c3e6dab |
|
MD5 | 722e79a2084a3f1069387bc1265343df |
|
BLAKE2b-256 | cb9402060e041351d91e6820ca8b740235dea6235d8bedf5ab2c8c86b73581fc |