Distributed semaphore utility based on NATS JetStream key-value stores.
Project description
nats-semaphore
A distributed semaphore implementation for Python using NATS JetStream KeyValue stores. This library allows you to coordinate access to shared resources across multiple processes or services with ease.
Features
- Distributed Locking: Leverages NATS JetStream KeyValue stores for reliable distributed coordination.
- Configurable Concurrency: Define the number of slots (concurrency limit) for each semaphore.
- Asyncio Support: Built from the ground up for Python's
asyncio. - Context Manager: Easy-to-use
async withsyntax for automatic lock acquisition and release. - Timeout Handling: Support for acquisition timeouts.
Installation
Coming soon(tm)!
Usage
Here is a simple example of how to use nats-semaphore:
import asyncio
import nats
from nats_semaphore import NatsSemaphoreContext
async def main():
# 1. Connect to NATS
nc = await nats.connect("nats://localhost:4222")
# 2. Initialize the context
# 'kv' is the name of the NATS KeyValue bucket to use for storing locks.
# It will be created if it doesn't exist.
semaphore_context = NatsSemaphoreContext(nc, kv="SEMAPHORE_BUCKET")
# 3. Define a semaphore
# 'name' identifies the resource.
# 'slot_count' is the maximum number of concurrent locks allowed.
semaphore = semaphore_context.semaphore(name="my-shared-resource", slot_count=3)
# 4. Acquire a lock using a context manager
try:
# Try to acquire a lock, waiting up to 5 seconds
async with semaphore.lock(timeout=5.0) as lock:
print("Lock acquired! Doing work...")
await asyncio.sleep(1)
print("Work done.")
except asyncio.TimeoutError:
print("Failed to acquire lock within timeout.")
# Alternative: Manual acquire/release
try:
lock = await semaphore.acquire(timeout=5.0)
print("Manually acquired lock.")
# ... do work ...
finally:
await lock.release()
print("Manually released lock.")
await nc.close()
if __name__ == "__main__":
asyncio.run(main())
Requirements
- Python >= 3.10
- nats-py >= 2.12
Compatibility
Compatible with nats-server versions:
- 2.7
- 2.8
- 2.9
- 2.10
- 2.11
- 2.12
License
Apache-2.0
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nats_semaphore-0.0.1.tar.gz.
File metadata
- Download URL: nats_semaphore-0.0.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
546c55294c9ace6056a933e830beb79fe30fe2ace3dc28d88a701264c957a05e
|
|
| MD5 |
2440fae2dea7c3c8c98970f5ebf5c1d8
|
|
| BLAKE2b-256 |
7100c1ee54e809b95bf8eb5112843512421d164d7af94386ec9647e7c3927a8c
|
Provenance
The following attestation bundles were made for nats_semaphore-0.0.1.tar.gz:
Publisher:
publish.yml on m3nowak/nats-semaphore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nats_semaphore-0.0.1.tar.gz -
Subject digest:
546c55294c9ace6056a933e830beb79fe30fe2ace3dc28d88a701264c957a05e - Sigstore transparency entry: 717699901
- Sigstore integration time:
-
Permalink:
m3nowak/nats-semaphore@2cf04f046c3718b43ad1f08ad0d2b34a04ad2604 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/m3nowak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2cf04f046c3718b43ad1f08ad0d2b34a04ad2604 -
Trigger Event:
release
-
Statement type:
File details
Details for the file nats_semaphore-0.0.1-py3-none-any.whl.
File metadata
- Download URL: nats_semaphore-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5f335e5c2896f27382d2eee1d95cfc5c429250116b0b669673a952c2f17cafa
|
|
| MD5 |
f2bb564ba7a7c99063ad4a893260af87
|
|
| BLAKE2b-256 |
d4bc610db69873d0912595ed98a135a3aaabf66e8753736319a6279ac4072503
|
Provenance
The following attestation bundles were made for nats_semaphore-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on m3nowak/nats-semaphore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nats_semaphore-0.0.1-py3-none-any.whl -
Subject digest:
d5f335e5c2896f27382d2eee1d95cfc5c429250116b0b669673a952c2f17cafa - Sigstore transparency entry: 717699942
- Sigstore integration time:
-
Permalink:
m3nowak/nats-semaphore@2cf04f046c3718b43ad1f08ad0d2b34a04ad2604 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/m3nowak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2cf04f046c3718b43ad1f08ad0d2b34a04ad2604 -
Trigger Event:
release
-
Statement type: