Home > Backend Development > Python Tutorial > 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
Release: 2024-11-14 17:44:02
Original
772 people have browsed it

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)
Copy after login

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}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template