Skip to main content

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.

  • Language: Python 3.10+
  • Package manager: uv
  • Status: Library + unit tests
  • License: See LICENSE

Features

  • Atomic lock acquisition
    Uses GCS’s ifGenerationMatch=0 conditional write to ensure collision-free lock acquisition.
  • Safe lock updates
    Updates metadata with ifMetagenerationMatch to detect changes made by other processes.

How It Works

  1. Check the lock object

    • GET the lock object from the bucket.
    • If not found → lock is free (or expired).
    • If found → read generation, metageneration, and metadata.
  2. Acquire the lock

    • PUT with ifGenerationMatch=0.
    • If the object already exists → 412 Precondition Failed → acquisition fails.
    • If created successfully (200 OK) → lock acquired.
  3. Update the lock (optional)

    • PATCH with ifMetagenerationMatch=<current>.
    • If another process updated it → 412 → treat as contention.
    • On success (200 OK) → metadata updated.
  4. 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(lock_owner=OWNER)

try:
    with lock.acquire(
        bucket_name=BUCKET, object_key=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

gcslock-0.0.5.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

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

gcslock-0.0.5-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file gcslock-0.0.5.tar.gz.

File metadata

  • Download URL: gcslock-0.0.5.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gcslock-0.0.5.tar.gz
Algorithm Hash digest
SHA256 df0d3a9354d1b156b9322f66ec3d79a0e21489604319b4047f998c9f22368a83
MD5 6e52e715ffa8a9ff2b5f204645e8b4ce
BLAKE2b-256 8677e7f52ea65dd44f5beb610292a3b329ebc6235f831589d32567279a03882f

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcslock-0.0.5.tar.gz:

Publisher: publish-pypi.yml on minthem/gcs-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 gcslock-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: gcslock-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gcslock-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7d7800cb20c220ac17590511032c911c472d004100cce96961ced0953ff7ceca
MD5 32060e296f42f5bf79381ba58132ae1d
BLAKE2b-256 0dcc8a82e8bba72a817fd7ff3725011fcfe7b2393262bf4dfea69492b36a5f24

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcslock-0.0.5-py3-none-any.whl:

Publisher: publish-pypi.yml on minthem/gcs-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