Skip to main content

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

Metadata

Release files for shared_event 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shared_event 0.1.0
File Size Uploaded Provenance Actions
shared_event-0.1.0.tar.gz 6.9 kB Without provenance Details for shared_event-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shared_event 0.1.0
File Interpreter ABI Platform Actions
shared_event-0.1.0-py3-none-any.whl Python 3 none any Details for shared_event-0.1.0-py3-none-any.whl

Total release size: 11.5 kB

Release files / shared_event-0.1.0.tar.gz

Download URL shared_event-0.1.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4e5650b5fc805f7530bf581fbfcd037e6b73495f00443494b7803eff726b1540
BLAKE2b-256 checksum
How to use checksums
bd9fb8887311b1cf0a360a7d3046b6089091edec8fd703a72b1fc894881d10f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.19

Release files / shared_event-0.1.0-py3-none-any.whl

Download URL shared_event-0.1.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a01c5032d4416d1866130a15fc82e56c3140c17772ae551900e4aef247cc5d2
BLAKE2b-256 checksum
How to use checksums
0afacb080f39bc605b49ed4c284937d1b90fea8602f6ad2bbc341c0877592fcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.19

Security for shared_event 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page