A distributed lock implementation based on SQLAlchemy
Project description
SQLAlchemy-DLock
Distributed lock based on Database and SQLAlchemy.
It currently supports below locks:
Database | Lock |
---|---|
MySQL | named lock |
PostgreSQL | advisory lock |
Usages
-
Work with SQLAlchemy
Connection
:from sqlalchemy import create_engine from sqlalchemy_dlock import create_sadlock key = 'user/001' engine = create_engine('postgresql://scott:tiger@localhost/') conn = engine.connect() # Create the D-Lock on the connection lock = create_sadlock(conn, key) # it's not lock when constructed assert not lock.acquired # lock lock.acquire() assert lock.acquired # un-lock lock.release() assert not lock.acquired
-
with
statementfrom contextlib import closing from sqlalchemy import create_engine from sqlalchemy_dlock import create_sadlock key = 'user/001' engine = create_engine('postgresql://scott:tiger@localhost/') with engine.connect() as conn: # Create the D-Lock on the connection with create_sadlock(conn, key) as lock: # It's locked assert lock.acquired # Auto un-locked assert not lock.acquired # If do not want to be locked in `with`, a `closing` wrapper may help with closing(create_sadlock(conn, key)) as lock2: # It's NOT locked here assert not lock2.acquired # lock it now: lock2.acquire() assert lock2.acquired # Auto un-locked assert not lock2.acquired
-
Work with SQLAlchemy
ORM
session:from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy_dlock import create_sadlock key = 'user/001' engine = create_engine('postgresql://scott:tiger@localhost/') Session = sessionmaker(bind=engine) with Session() as session: with create_sadlock(session, key) as lock: assert lock.acquired assert not lock.acquired
-
Asynchronous I/O Support
ℹ️ info:
from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy_dlock.asyncio import create_async_sadlock key = 'user/001' engine = create_async_engine('postgresql+asyncpg://scott:tiger@localhost/') async with engine.begin() as conn: async with create_async_sadlock(conn, key) as lock: assert lock.locked await lock.release() assert not lock.locked await lock.acquire() assert not lock.locked
Tests
Following SQLAlchemy engines are tested:
You can run unit-tests:
-
directly:
-
Install the project (A virtual environment (venv) is strongly advised):
pip install -e .
-
Start up your mysql and postgresql
-
Set environment variables
TEST_URLS
andTEST_ASYNC_URLS
for sync and async database connection url. Multiple connections separated by space. The test cases load environment variables intests/.env
.eg (and also the defaults):
TEST_URLS=mysql://test:test@localhost/test postgresql://postgres:test@localhost/ TEST_ASYNC_URLS=mysql+aiomysql://test:test@localhost/test postgresql+asyncpg://postgres:test@localhost/
-
run unit-test
python -m unittest
-
-
in docker-compose:
-
build the project
python -m pip install build && python -m build -w
-
run unit-test
cd tests docker compose up --abort-on-container-exit docker compose rm -fsv
-
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.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 247a8ec55a7ea72c08facd790bab13d3e88125881a0f25c9a44773dd2868eea4 |
|
MD5 | 4df968b9fe7e1236631762349e095e03 |
|
BLAKE2b-256 | 7cc1f558a13b5c29fe4c6a43b7e8fe9e76a51637815590c974f4c0aa1570df07 |