Skip to main content

PostgreSQL-based distributed advisory lock (sync + async).

Project description

pg-dlock

PostgreSQL advisory-lock based distributed lock, sync + async.

from pg_dlock import Locker

with Locker("postgres://...") as locker:
    with locker.lock("my-key"):
        ...  # critical section

Async:

from pg_dlock import AsyncLocker

async with AsyncLocker("postgres://...") as locker:
    async with locker.lock("my-key"):
        ...

API

  • Locker(conninfo, *, pool_min_size=0, pool_max_size=10)
  • locker.lock(key, *, scope="session"|"transaction", shared=False) -> Lock
  • lock.try_acquire(timeout=None) -> bool
    • timeout=None: block forever
    • timeout=0: non-blocking
    • timeout>0 (seconds): wait up to N seconds (uses statement_timeout)
  • lock.release()
  • Context manager support (with lock: / async with lock:).

Scopes

  • session: each lock opens its own dedicated connection (autocommit). Released via pg_advisory_unlock.
  • transaction: connection is checked out from an internal pool and a transaction is begun; lock is released by the commit/rollback on exit.

Keys

  • str / bytes: hashed with xxhash.xxh3_64 into a signed int64.
  • int: used directly (must fit in signed int64).
  • tuple[int, int]: each element must fit in signed int32; packed into a single int64.

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

pg_dlock-0.0.0a1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

pg_dlock-0.0.0a1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pg_dlock-0.0.0a1.tar.gz.

File metadata

  • Download URL: pg_dlock-0.0.0a1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pg_dlock-0.0.0a1.tar.gz
Algorithm Hash digest
SHA256 0e0856bb76bc82122601d276cfc21ba485f373603d52f45f7a221c28168fa054
MD5 a0099cfffc29f26b2c435d94961ed480
BLAKE2b-256 9ffda0b86ba90797e1789d922a4d70f1018577cedd1e3d6656fe66c871ef5886

See more details on using hashes here.

File details

Details for the file pg_dlock-0.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: pg_dlock-0.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pg_dlock-0.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 e6c4d5aaf5e1707191ffaaf26b6049a77e89d4c07b6d71d496e8b288896559a7
MD5 28be04eea06669c82f08170a06bcdf30
BLAKE2b-256 3a28dfd53484a21fe02ef657bc91c23f94c3423d71256a6184161a4bf8729374

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