Registry / aws / python-dynamodb-lock

python-dynamodb-lock

JSON →
library0.9.1pypypiunverified

Python DynamoDB Lock is a general-purpose distributed locking library built on top of DynamoDB. It supports both coarse-grained and fine-grained locking, heavily inspired by the Java-based AmazonDynamoDBLockClient. The current version is 0.9.1, with the last release in October 2018. While functional, its development appears to be in maintenance mode.

pip install python-dynamodb-lock
INSTALL
IMPORT
SIG · PYTHON-DYNAMODB-LO
P
python-dynamodb-lock
awspythonv0.9.1
Install
3.8s avg
Import
Disk
49MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 50.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.8s · import 0.000s · 51MB
49MB installed
● package 49MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DynamoDBLockClient
from python_dynamodb_lock import DynamoDBLockClient
from python_dynamodb_lock import DynamoDBLockClient

This quickstart demonstrates how to acquire and release a distributed lock using `DynamoDBLockClient` as a context manager for a specific resource. It includes basic setup for a DynamoDB table and shows how to handle the critical section.

import boto3 from python_dynamodb_lock import DynamoDBLockClient import os import time # Configure AWS credentials and region (e.g., via environment variables or ~/.aws/credentials) # For local testing, you might use 'local' endpoint_url dynamodb_resource = boto3.resource( 'dynamodb', region_name=os.environ.get('AWS_REGION', 'us-east-1'), endpoint_url=os.environ.get('DYNAMODB_ENDPOINT_URL', None) ) table_name = os.environ.get('DYNAMODB_LOCK_TABLE_NAME', 'MyDistributedLockTable') # It's recommended to create the table beforehand with a 'lock_key' primary key and TTL enabled # For demonstration, we'll try to create it if it doesn't exist try: table = dynamodb_resource.Table(table_name) table.load() except dynamodb_resource.meta.client.exceptions.ResourceNotFoundException: print(f"Creating DynamoDB table: {table_name}") dynamodb_resource.create_table( TableName=table_name, KeySchema=[{'AttributeName': 'lock_key', 'KeyType': 'HASH'}], AttributeDefinitions=[{'AttributeName': 'lock_key', 'AttributeType': 'S'}], BillingMode='PAY_PER_REQUEST' ) # Wait for the table to be active dynamodb_resource.meta.client.get_waiter('table_exists').wait(TableName=table_name) print(f"Table {table_name} created.") # Instantiate the lock client # The 'owner_name' helps identify who holds the lock (e.g., hostname + process ID) lock_client = DynamoDBLockClient( dynamodb_resource, table_name=table_name, owner_name=f"{os.uname()[1]}-{os.getpid()}" ) lock_key = "my-critical-resource" try: print(f"Attempting to acquire lock for {lock_key}...") # Use the 'lock' method as a context manager for a specific lock with lock_client.lock(lock_key): print(f"Successfully acquired lock for {lock_key}. Performing critical section...") # Simulate work time.sleep(5) print(f"Finished critical section for {lock_key}.") print(f"Lock for {lock_key} automatically released.") except Exception as e: print(f"Failed to acquire or process with lock for {lock_key}: {e}") finally: # It's important to close the client to stop background heartbeat threads print("Closing lock client...") lock_client.close() print("Lock client closed.")
Debug
Known issues
breakingThe library's last release was in October 2018. While functional, it may not receive active updates or support for newer Python versions/AWS features.
fix
Evaluate against modern alternatives or consider contributing to the project if extended functionality or maintenance is required.
affects: <=0.9.1
gotchaThis library does NOT participate in distributed transactions. If an application holding a lock experiences prolonged delays (e.g., GC pauses, errors) preventing heartbeats, another client might assume the lock is abandoned and acquire it. The original client might then commit changes even after its lock has been 'stolen', leading to data inconsistencies.
fix
Design your application with eventual consistency in mind or implement application-level checks to handle potential conflicts if a lock is stolen. Do not assume transactional guarantees across lock acquisition and business logic.
affects: All
gotchaSudden process termination of a client will leave its acquired locks in DynamoDB until they eventually expire based on their Time-To-Live (TTL) attribute. This expiry is not immediate and can take up to 24 hours.
fix
Ensure your DynamoDB table has a TTL attribute configured (default: 'expiry_time') and that `expiry_period` is set appropriately during client instantiation to allow timely cleanup of abandoned locks. Implement robust error handling and graceful shutdown procedures where possible.
affects: All
gotchaCalling `lock_client.close()` by default does NOT release all locks owned by that client. This design prevents premature lock release while application logic might still be processing under the assumption of holding the lock.
fix
Explicitly manage lock release for individual `DynamoDBLock` instances. If you need to force release all locks on shutdown, you might need to iterate through and release them, or rely on TTL for cleanup (see previous warning).
affects: All
gotchaThe DynamoDB table used for locking must be configured with a primary key (default: 'lock_key' of type String) and it is highly recommended to enable and configure a TTL attribute (default: 'expiry_time') to allow DynamoDB to automatically clean up old/abandoned lock entries.
fix
Before using the library, ensure your DynamoDB table adheres to the required schema and has TTL enabled. Example table creation is provided in the quickstart.
affects: All
Upgrade
Version history
0.9.1latest on PyPI · released Oct 28, 2018
Audit
Dependencies
boto3requiredRequired for interacting with AWS DynamoDB.
Agent activity
23 hits · last 30 days
node
22
Resources