Skip to main content

A shared-memory based multiprocessing event using atomics

Project description

Shared Event

A shared-memory based multiprocessing event for cross-process synchronization. Unlike multiprocessing.Event, this event is fully picklable and can be shared between processes by name.

Features

  • Picklable: Can be safely passed between processes via pickle
  • Named events: Multiple processes can connect to the same event by name
  • Shared memory backed: Uses multiprocessing.shared_memory for efficient IPC
  • Simple API: Drop-in replacement for threading.Event / multiprocessing.Event
  • Run namespacing: Isolate events by run_id to prevent collisions

Installation

pip install shared_event

Or for development:

git clone <repo-url>
cd shared_event
uv sync
uv pip install -e .

Quick Start

from shared_event import SharedMemoryEvent
from multiprocessing import Process
import time

def worker(event_name: str, run_id: str):
    # Connect to existing event by name
    event = SharedMemoryEvent(name=event_name, create=False, run_id=run_id)

    print("Worker waiting for signal...")
    event.wait()  # Block until event is set
    print("Worker received signal!")

    event.close()

def main():
    run_id = "my_app"

    # Create the event in main process
    ready_event = SharedMemoryEvent(name="ready", create=True, run_id=run_id)

    # Start worker process
    p = Process(target=worker, args=("ready", run_id))
    p.start()

    # Do some work...
    time.sleep(2)

    # Signal the worker
    print("Signaling worker...")
    ready_event.set()

    p.join()

    # Cleanup
    ready_event.unlink()  # Delete shared memory

if __name__ == "__main__":
    main()

API Reference

SharedMemoryEvent(name, create=False, run_id="")

Creates or connects to a shared memory event.

Parameters:

  • name (str): Event identifier
  • create (bool): Whether to create new event (True) or connect to existing (False)
  • run_id (str): Optional run identifier for namespacing events

Methods:

  • set(): Set the event (wake up all waiters)
  • clear(): Clear the event
  • is_set() -> bool: Check if event is currently set
  • wait(timeout=None) -> bool: Wait for event to be set. Returns True if set, False on timeout
  • close(): Close connection to shared memory
  • unlink(): Delete the shared memory segment (call from creator process)

Use Cases

1. Process Coordination

# Coordinator process
coordinator_event = SharedMemoryEvent("start_processing", create=True, run_id="batch_job")

# Worker processes connect by name
worker_event = SharedMemoryEvent("start_processing", create=False, run_id="batch_job")
worker_event.wait()  # Wait for coordinator signal

2. Replacing Non-Picklable Events

# Instead of this (won't work across process boundaries):
# event = multiprocessing.Event()  # Can't pickle reliably

# Use this:
event = SharedMemoryEvent("sync_point", create=True, run_id="app")
# This event can be safely pickled and passed to subprocesses

3. Multiple Event Coordination

# Setup phase
setup_done = SharedMemoryEvent("setup", create=True, run_id="pipeline")
data_ready = SharedMemoryEvent("data", create=True, run_id="pipeline")
processing_done = SharedMemoryEvent("processing", create=True, run_id="pipeline")

# Workers connect to relevant events
setup_event = SharedMemoryEvent("setup", create=False, run_id="pipeline")
setup_event.wait()  # Wait for setup to complete

Implementation Notes

  • Uses multiprocessing.shared_memory for efficient cross-process communication
  • Currently uses polling with 1ms sleep for wait() - futex support planned for more efficient waiting
  • Each event uses 1 byte of shared memory
  • Event names are automatically prefixed with run_id for isolation
  • Supports both timeout and indefinite waiting
  • Proper cleanup with close() and unlink() methods

Development

# Install dependencies
uv sync

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=shared_event

# Type checking
uv run mypy .

# Linting
uv run ruff check .

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_event-0.1.0.tar.gz (6.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_event-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file shared_event-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for shared_event-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4e5650b5fc805f7530bf581fbfcd037e6b73495f00443494b7803eff726b1540
MD5 7a0670a69594adb9ba5f09bc83f47c25
BLAKE2b-256 bd9fb8887311b1cf0a360a7d3046b6089091edec8fd703a72b1fc894881d10f2

See more details on using hashes here.

File details

Details for the file shared_event-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for shared_event-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a01c5032d4416d1866130a15fc82e56c3140c17772ae551900e4aef247cc5d2
MD5 48ddef6a86d4aa0d96e8f764cdd932f1
BLAKE2b-256 0afacb080f39bc605b49ed4c284937d1b90fea8602f6ad2bbc341c0877592fcb

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