Home >Backend Development >Python Tutorial >How can I ensure data consistency in a global dictionary accessed by multiple child processes?
Multiprocessing: Addressing Synchronization and Data Sharing in Global Dictionaries
In a multithreaded environment, handling data concurrent access and maintaining synchronization becomes crucial. Consider a program with multiple child processes working on a queue and manipulating a global dictionary, D.
When a child process modifies D, these updates are visible within the process. However, after the main process joins the queue, printing D in the main process reveals an empty dictionary. This is caused by synchronization issues in accessing the shared resource, D.
To resolve this, a Manager object can be employed. The Manager class in multiprocessing enables the creation and management of shared objects, including dictionaries. The following adjusted python code demonstrates its usage:
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)
By utilizing the Manager object, the shared dictionary, D, is stored in a shared memory location that is accessible to all processes. This ensures synchronization and prevents race conditions in accessing the dictionary, even across multiple processes.
Running this modified code should yield the following output:
{1: '111', '2': 6}
demonstrating that the modifications made by child processes in the shared dictionary are visible and persistent even after joining the processes.
The above is the detailed content of How can I ensure data consistency in a global dictionary accessed by multiple child processes?. For more information, please follow other related articles on the PHP Chinese website!