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.1.tar.gz (7.8 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.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for shared_memory_lock-0.1.1.tar.gz
Algorithm Hash digest
SHA256 18b55c56c640b99fbd083a31045d4aa061ddec1ec68bd77eff58ad02fafc9b67
MD5 c1f52eef3c8c72952f118286102994f5
BLAKE2b-256 1b3cd841a9b539ef3c46ff6284eb4afe9699b1f01660345fcd238b75a33639e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for shared_memory_lock-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4c29a609a3e54609a85beeb05f01953a088203cdd2d6399a5b61b6268023c0f
MD5 e48e04962c98539dccf1a1709a2c23f0
BLAKE2b-256 9cdc500aa5e779d26e0733126485963ece166f4043e4fecc7caecfdbfd21a956

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