How to manage a state between processes
Python multiprocessing provides a manager to coordinate shared information between all its users. A manager object controls a server process that holds Python objects and allows other processes to manipulate them.
A manager has the following properties:
It controls the server process that manages a shared object
It makes sure the shared object gets updated in all processes when anyone modifies it
How to do it...
Let's see an example of how to share a state between processes:
First, the program creates a manager list, shares it between n number of
taskWorkers
, and every worker updates an index.After all workers finish, the new list is printed to
stdout
:import multiprocessing def worker(dictionary, key, item): dictionary[key] = item if __name__ == '__main__': mgr = multiprocessing.Manager() dictionary = mgr.dict() jobs = [ multiprocessing.Process\ (target=worker, args=(dictionary, i, i*2)) for i in range(10) ...