Skip to main content

A distributed lock implementation based on SQLAlchemy

Project description

SQLAlchemy-DLock

Documentation Status

Distributed lock based on Database and SQLAlchemy.

It currently supports blow 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:

    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.acquired
        assert not lock.acquired
    

CHANGELOG

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.2a2.tar.gz (11.1 kB view hashes)

Uploaded Source

Built Distribution

SQLAlchemy_DLock-0.2a2-py3-none-any.whl (17.0 kB view hashes)

Uploaded Python 3

Supported by

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