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
v0.2a1
Date: 2021-03-08
-
New:
-
Asynchronous IO Support by:
-
aiomysql for MySQL
Connection URL is like:
"mysql+aiomysql://user:password@host:3306/schema?charset=utf8mb4"
-
asyncpg for PostgreSQL
Connection URL is like:
"postgresql+asyncpg://user:password@host:5432/db"
Read https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html for details
-
-
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
Hashes for SQLAlchemy_DLock-0.2a1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 199f029bae25c0a9a0d995d71d1463d9257cbc5927218304bcbed704cf25efa7 |
|
MD5 | 4624f758472a384eb10a8a314bac7f25 |
|
BLAKE2b-256 | 4ed21a8c6003392b860422378582f7efef03f8c68010b332de31c7480e891e5a |