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.4.tar.gz (12.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.4-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for shared_memory_lock-0.1.4.tar.gz
Algorithm Hash digest
SHA256 809148e50f929a3124be1b8624f583661e14f6fc59302c525c92bc601d86bbf1
MD5 cb748fee149aba78bcd1bcad71a1b0f9
BLAKE2b-256 07d439c53bd94b693906f3c3e58fb0a99dfe147009efe077b8a6d5bcb65da69a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for shared_memory_lock-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c1617c56f5570456dab3ccdb1a3ad1a1f2be297c310f4b8fd39701bc80620452
MD5 c028c0388db9fabfcc4ecffe268ced60
BLAKE2b-256 c2633cf9fd20befac901ec0988b35ada6360e77d70e37052b41018988783c94a

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