Skip to main content

Distributed lock using NATS.io

Project description

nats-kv-lock

A simple, distributed lock using NATS.io

Note!

  • This is a simple PoC and perhaps shouldn't be used in a production environment
  • This does NOT guarantee order, i.e. it is an unfair lock.

Installation

Assuming you already have NATS installed, you can simply pip install nats-kv-lock

Example

All you need to do is create a KV bucket, then initialize an instance of NatsKvLock using that bucket with a shared lock_name.

import asyncio

from nats import connect
from nats_kv_lock import NatsKvLock

async def main():
    print('connecting...')
    nc = await connect()
    print('connected!')
    
    js = nc.jetstream()
    try:
        kv = await js.key_value(bucket='my_locks')
    except:
        kv = await js.create_key_value(bucket='my_locks')
    
    my_lock = NatsKvLock(kv, 'my_lock')

    print('acquiring lock...')
    
    async with my_lock:
        print('acquired! doing work...')
        await asyncio.sleep(10)
        print('releasing lock...')

    print('released lock!')

if __name__ == '__main__':
    asyncio.run(main())

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

nats_kv_lock-0.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

nats_kv_lock-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file nats_kv_lock-0.0.1.tar.gz.

File metadata

  • Download URL: nats_kv_lock-0.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nats_kv_lock-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2e8bcf7629fbfc4805893e6901223fffcad2da6c4dd62ad4022a2f914b6637c2
MD5 d0ff355e408e432e5b0348eb3a8af40c
BLAKE2b-256 ca0ddfcbd3ffc3d71f15e79f0108a0d9950b3758e59e379160ad7b50fb6f6671

See more details on using hashes here.

Provenance

The following attestation bundles were made for nats_kv_lock-0.0.1.tar.gz:

Publisher: python-publish.yml on kevinhikaruevans/nats-kv-lock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nats_kv_lock-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: nats_kv_lock-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nats_kv_lock-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bad2c86ee6aa3208d3a67e5c4b26b9d924b0387c44655a95bb822dfda083caba
MD5 5be78e048b4c0e52b08d387136c68004
BLAKE2b-256 13783e977313128d21a979ad4d15d47e4bbb2e31684cc14a0d7f8738028d8287

See more details on using hashes here.

Provenance

The following attestation bundles were made for nats_kv_lock-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on kevinhikaruevans/nats-kv-lock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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