Add your description here
Project description
gcs-atomic-lock
gcs-atomic-lock is a Python library that uses Google Cloud Storage (GCS) object generations and *
metagenerations* to implement a lightweight distributed lock with strong, atomic semantics.
It wraps GCS’s conditional write APIs and exposes a simple Python context manager for acquiring, updating, and releasing
a lock.
Features
- Atomic lock acquisition
Uses GCS’sifGenerationMatch=0conditional write to ensure collision-free lock acquisition. - Safe lock updates
Updates metadata withifMetagenerationMatchto detect changes made by other processes.
How It Works
-
Check the lock object
- GET the lock object from the bucket.
- If not found → lock is free (or expired).
- If found → read
generation,metageneration, andmetadata.
-
Acquire the lock
- PUT with
ifGenerationMatch=0. - If the object already exists → 412 Precondition Failed → acquisition fails.
- If created successfully (200 OK) → lock acquired.
- PUT with
-
Update the lock (optional)
- PATCH with
ifMetagenerationMatch=<current>. - If another process updated it → 412 → treat as contention.
- On success (200 OK) → metadata updated.
- PATCH with
-
Release the lock
- DELETE the object (204 No Content on success).
- Optionally verify ownership via stored metadata before deletion.
Flow
flowchart
A[Start] --> B{GET Bucket}
B -- Not Found --> Z[Bucket Not Found Error]
B -- Found --> C{GET Object}
C -- Not Found --> D[PUT with ifGenerationMatch=0]
D -- 412 --> E[LOCK FAILED]
D -- 200 --> F[LOCK SUCCESS]
C -- Found --> G[PATCH with ifMetagenerationMatch=CurrentValue]
G -- 412 --> E
G -- 200 --> F
F --> H[DELETE Object to Unlock]
H -- 204 --> I[LOCK RELEASED]
Installation
pip install gcslock
Example
Authentication Requirement
Before running the example below, make sure you have authenticated with Google Cloud. You can either:
- Run
gcloud auth application-default login
- Or create a Credentials instance using a Service Account key JSON file.
Without authentication, the library will not be able to access GCS.
Code Example
from gcslock import GcsLock, LockState
from gcslock.exception import GcsLockError
from google.oauth2.service_account import Credentials
# Optional: Authenticate with Google Cloud Service Account key JSON file
#cred = Credentials.from_service_account_file("path/to/service_account.json")
BUCKET = "your-bucket-name"
OBJECT = "locks/my-resource.lock"
OWNER = "your-owner-id"
LOCK_EXPIRES_SEC = 60
lock = GcsLock(
bucket_name=BUCKET,
lock_owner=OWNER
)
try:
with lock.acquire(
lock_id=OBJECT,
expires_seconds=LOCK_EXPIRES_SEC
) as lock:
# Critical section
...
except GcsLockError as e:
print(
f"Lock error: {e}"
)
Use Cases
- Preventing concurrent execution in distributed job schedulers
- Coordinating resource access across multiple instances
- Building a lightweight distributed locking mechanism using GCS
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
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 gcslock-0.0.3.tar.gz.
File metadata
- Download URL: gcslock-0.0.3.tar.gz
- Upload date:
- Size: 23.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b32cc522b0cab4a363892a16f1123d88060e41985685b7a5e15ad05f17f6df0
|
|
| MD5 |
fcaf2221c193fcd574a28c76582bb871
|
|
| BLAKE2b-256 |
cedac4c4a7d6625eb3e5d00bb06a159704cf05f64bcfd19bf2c968c7bfa23790
|
Provenance
The following attestation bundles were made for gcslock-0.0.3.tar.gz:
Publisher:
publish-pypi.yml on minthem/gcs-lock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gcslock-0.0.3.tar.gz -
Subject digest:
2b32cc522b0cab4a363892a16f1123d88060e41985685b7a5e15ad05f17f6df0 - Sigstore transparency entry: 453544788
- Sigstore integration time:
-
Permalink:
minthem/gcs-lock@8db7b47d6608a1e4e39a867128683c71993fb6ce -
Branch / Tag:
refs/tags/0.0.3 - Owner: https://github.com/minthem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@8db7b47d6608a1e4e39a867128683c71993fb6ce -
Trigger Event:
push
-
Statement type:
File details
Details for the file gcslock-0.0.3-py3-none-any.whl.
File metadata
- Download URL: gcslock-0.0.3-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d05efcaab11f39cc1b4d7e8f3e90918e2c156823b2145f42d7a9b322564d720b
|
|
| MD5 |
5d8564486cf55a2dbc17fa7994e652c3
|
|
| BLAKE2b-256 |
09c3c94eeaad187a9a268ae2304b896a0bedfe5d47e3e685674d63e4e7b6fb2b
|
Provenance
The following attestation bundles were made for gcslock-0.0.3-py3-none-any.whl:
Publisher:
publish-pypi.yml on minthem/gcs-lock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gcslock-0.0.3-py3-none-any.whl -
Subject digest:
d05efcaab11f39cc1b4d7e8f3e90918e2c156823b2145f42d7a9b322564d720b - Sigstore transparency entry: 453544789
- Sigstore integration time:
-
Permalink:
minthem/gcs-lock@8db7b47d6608a1e4e39a867128683c71993fb6ce -
Branch / Tag:
refs/tags/0.0.3 - Owner: https://github.com/minthem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@8db7b47d6608a1e4e39a867128683c71993fb6ce -
Trigger Event:
push
-
Statement type: