Home  >  Article  >  Backend Development  >  How to Share and Modify a Dictionary Between Multiple Processes in Python?

How to Share and Modify a Dictionary Between Multiple Processes in Python?

Susan Sarandon
Susan SarandonOriginal
2024-11-14 17:44:02710browse

How to Share and Modify a Dictionary Between Multiple Processes in Python?

Multiprocessing: Inter-Process Dictionary Sharing

Consider a scenario where multiple processes operate on a shared queue, Q, and manipulate a global dictionary, D. Surprisingly, accessing D after joining Q reveals an empty dictionary, despite observed modifications in child processes.

This behavior stems from synchronization issues. Each process operates on its own memory space, and changes made to D within child processes are not automatically reflected in the main process. To address this, synchronization measures are necessary.

Solution: Using a Manager Object

The Python standard library provides a solution using the Manager object. This object offers synchronized access to shared data among processes:

from multiprocessing import Process, Manager

def f(d):
    d[1] += '1'
    d['2'] += 2

if __name__ == '__main__':
    manager = Manager()

    d = manager.dict()
    d[1] = '1'
    d['2'] = 2

    p1 = Process(target=f, args=(d,))
    p2 = Process(target=f, args=(d,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

    print(d)

In this example, the Manager object's dict() method creates a shared dictionary, d, which is accessible by all processes. Changes made to d within the child processes are propagated to the main process through the shared memory managed by the Manager object.

Output:

$ python mul.py
{1: '111', '2': 6}

The above is the detailed content of How to Share and Modify a Dictionary Between Multiple Processes in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn