Skip to main content

A multiprocessing lock implemented using shared memory and atomics.

Project description

Shared Memory Lock

A multiprocessing lock implemented using shared memory and atomics.

Installation

uv pip install -e .

Usage

from shared_memory_lock import SharedMemoryLock

# Create a lock (first process)
lock = SharedMemoryLock(name="my_lock", create=True, run_id="app1")

# Connect to the same lock (other processes)
lock = SharedMemoryLock(name="my_lock", create=False, run_id="app1")

# Use as context manager
with lock:
    # Critical section - only one process can be here
    print("Doing important work...")

# Or manual acquire/release
if lock.acquire(blocking=False):
    try:
        # Got the lock
        print("Acquired lock!")
    finally:
        lock.release()
else:
    print("Could not acquire lock")

# Clean up when done
lock.close()
lock.unlink()  # Only call from one process

Features

  • Cross-process synchronization: Works across multiple processes
  • Atomic operations: Uses real atomic compare-and-swap for mutual exclusion
  • Picklable: Can be passed to multiprocessing.Process
  • Context manager: Supports with statements
  • Non-blocking: Optional non-blocking acquire with timeout

Development

# Install dependencies
uv sync

# Run tests
uv run --python 3.13.7 pytest 

# Run tests with coverage
uv run --python 3.13.7 pytest --cov=shared_memory_lock

License

MIT

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

shared_memory_lock-0.1.3.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

shared_memory_lock-0.1.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file shared_memory_lock-0.1.3.tar.gz.

File metadata

  • Download URL: shared_memory_lock-0.1.3.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for shared_memory_lock-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ab37dbef6bdf1d69654dfc904a44c0f1e72e361b9b13f2713924566ceb02f528
MD5 a46a1e9aaf2e9a4d767a4154db235da3
BLAKE2b-256 0c371b5fff3ae1b4a3f5779220d47bdbd8e8962a8a947f7ad97ca63e262eec34

See more details on using hashes here.

File details

Details for the file shared_memory_lock-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for shared_memory_lock-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c9ca3ca7c2c8c5c1f9c2d5cfb594b38b30cd139614f7e764c6e294164b452a54
MD5 a401fe7212905f0e85ff21a25a978269
BLAKE2b-256 e904c3e7ec99c1e91c09cba4697e45daaf72432ce54e7a7656987f163e7e4f7c

See more details on using hashes here.

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