Skip to main content

A distributed lock implementation based on SQLAlchemy

Project description

SQLAlchemy-DLock

GitHub GitHub tag (latest by date) Python package PyPI PyPI - Status PyPI - License PyPI - Python Version Documentation Status codecov

Distributed lock based on Database and SQLAlchemy.

It currently supports below locks:

Note:

The project is not stable enough and DO NOT use it in production.

Usages

  • Work with SQLAlchemy's Connection object:

    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
    
  • Use in with statement

    from 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's ORM session:

    Note:

    According to https://docs.sqlalchemy.org/14/orm/extensions/asyncio.html:

    • The asyncio extension as of SQLAlchemy 1.4.3 can now be considered to be beta level software.
    • The asyncio extension requires at least Python version 3.6
    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
    
  • Work asynchronously

    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

Just up the docker-compose in tests directory:

(cd tests; docker-compose up --abort-on-container-exit --exit-code-from pytest; docker-compose down)

CHANGELOG

v0.2b2/b3

Date: 2021-03-23

  • Add:
    • More unit tests
    • Optimized CI

v0.2b1

Date: 2021-03-16

  • Add:

    • New unit tests
    • CI by github workflows

v0.2a3

Date: 2021-03-14

  • Change:

    • Drop Python 3.5 support.
    • Remove SQLAlchemy version requires earlier than 1.4 in setup, it's not supported actually.
    • Adjust PostgreSQL lock's constructor arguments order
  • Add:

    • More test cases, and add test/deploy workflow in github actions.
    • Add docker-compose test scripts

v0.2a2

Date: 2021-03-09

  • Change:

    • Rename a lot of function/class:

      • sadlock -> create_sadlock
      • asyncio.sadlock -> asyncio.create_async_sadlock

      and some other ...

v0.2a1

Date: 2021-03-08

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's multiprossing.Lock, instead of Threading.Lock
    • MySQL lock now accepts float-point value as timeout
  • 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

Project details


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.2b3.post4.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_dlock-0.2b3.post4-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-dlock-0.2b3.post4.tar.gz.

File metadata

  • Download URL: sqlalchemy-dlock-0.2b3.post4.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for sqlalchemy-dlock-0.2b3.post4.tar.gz
Algorithm Hash digest
SHA256 ab68f9180d7b0223a66b3fe7aeba34fb376517bbe7c9241d22889270648943c1
MD5 49be1beea622853a9663362dd086b101
BLAKE2b-256 9a40c3518cd32733328e900a5b76f7ac251aacc4a53113a3f27b96269f2f979a

See more details on using hashes here.

File details

Details for the file sqlalchemy_dlock-0.2b3.post4-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_dlock-0.2b3.post4-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for sqlalchemy_dlock-0.2b3.post4-py3-none-any.whl
Algorithm Hash digest
SHA256 c7ae64ca8f723ba60cd025b040d1d851e2d3af7689c81597aa527a7b34f30795
MD5 003036d2fe6e988389d81b0eb1af2f58
BLAKE2b-256 5821d4056f9f0f99374c3c1436a858464e582b8af7b1b06ab2e60f61d9736101

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page