Skip to content

Manager in Python Multiprocessing

What is a Manager?

  • A Manager in Python's multiprocessing module provides a way to create and manage shared objects between processes.
  • Shared objects include lists, dictionaries, Events, Locks, and more.

Why Use a Manager?

  • Processes in Python have separate memory spaces.
  • Without a Manager, objects created in one process are not automatically shared with others.
  • A Manager allows processes to interact with shared objects safely.

Common Use Cases

  1. Sharing data structures like lists or dictionaries between processes.
  2. Synchronizing processes using shared objects like Event or Lock.

How to Use a Manager?

Basic Syntax

from multiprocessing import Manager

with Manager() as manager:
    shared_list = manager.list()  # Create a shared list
    shared_dict = manager.dict()  # Create a shared dictionary

Example: Shared Dictionary

from multiprocessing import Process, Manager

def worker(shared_dict):
    shared_dict["key"] = "value"  # Update the shared dictionary

if __name__ == "__main__":
    with Manager() as manager:
        shared_dict = manager.dict()  # Create a shared dictionary
        p = Process(target=worker, args=(shared_dict,))
        p.start()
        p.join()
        print(shared_dict)  # Output: {'key': 'value'}

Example: Shared Event

from multiprocessing import Process, Manager

def worker(event):
    print("Waiting for event...")
    event.wait()  # Wait for the event to be set
    print("Event is set!")

if __name__ == "__main__":
    with Manager() as manager:
        event = manager.Event()  # Create a shared Event
        p = Process(target=worker, args=(event,))
        p.start()
        event.set()  # Set the event
        p.join()

Advantages

  • Provides easy sharing of objects across processes.
  • Manages synchronization safely and avoids manual complexity.

Disadvantages

  • Slower than using shared memory (e.g., multiprocessing.Value or multiprocessing.Array) because communication is handled via a server process.
  • Overhead for small or frequent updates.

When to Use a Manager?

  • Use a Manager when you need to share complex data structures or synchronization primitives like Event or Lock.
  • For performance-critical tasks with simple data, prefer shared memory objects.