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.5.tar.gz (10.7 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.5-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for shared_memory_lock-0.1.5.tar.gz
Algorithm Hash digest
SHA256 08b08a5561a2c490a9630c587eadda8cfb5170263cbf83d311487f6f8ab2619d
MD5 5c2cff158dc8b31e18675db088b4a478
BLAKE2b-256 d37a87f15e041328a43c70e039ffb97967c22a68ccdee45383abf4d388d67be6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for shared_memory_lock-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fff1cadec203f0ac768c43d6bb34949aa0ced8b3c79d218445cea53d98c66345
MD5 6db0edebe4108f9dfeca75443f42e8aa
BLAKE2b-256 c7587d4ac30a152a1ee19a78788bf910da11f9acd6c4a6234fd4f6dd8cf3e3eb

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