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.2.tar.gz (12.5 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.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for shared_memory_lock-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b6ee46cf638ed83fd874c32e76d278c4048cb10dfa2f347d7ad8679b69209c96
MD5 30a79cd70b59343557c35753d8a5a7ca
BLAKE2b-256 947ee6c0f4d03f2d1bc316b55ba22db9fb6cfd1fcd1937ef41c96572cf81d596

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for shared_memory_lock-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 73293268255204c96b1d94c5d0736cdfa0392342e42e161a6167c504df1405e6
MD5 12e4ff1414236e36392b7d0e658bbd55
BLAKE2b-256 9c77b5616448a85bf231eaf20903fcc7968ffc72c96542ec6e9898ec03281057

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