Introduction to Python multiprocessing. Manager and instance (sharing data between processes), multiprocessing
In Python, processes share data and process basic queue, pipe, and value + array. It also provides high-level encapsulation. Use multiprocessing. Manager to easily use these advanced interfaces.
The Manager object returned by manager () controls a server process. The python object contained in this process can be accessed by other processes through proxies. To achieve data communication and security between multiple processes.
The types supported by Manager include list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value, and Array.
1) dict of Manager, used by list
Copy codeThe Code is as follows:
Import multiprocessing
Import time
Def worker (d, key, value ):
D [key] = value
If _ name _ = '_ main __':
Mgr = multiprocessing. Manager ()
D = mgr. dict ()
Jobs = [multiprocessing. Process (target = worker, args = (d, I, I * 2 ))
For I in range (10)
]
For j in jobs:
J. start ()
For j in jobs:
J. join ()
Print ('results :')
For key, value in enumerate (dict (d )):
Print ("% s = % s" % (key, value ))
# The output is:
# Results:
#0 = 0
#1 = 1
#2 = 2
#3 = 3
#4 = 4
#5 = 5
#6 = 6
#7 = 7
#8 = 8
#9 = 9
The above is the manager. dict instance.
2) The namespace object does not have a public method, but has writable attributes.
However, when using the proxy of the namespace returned by the manager, the _ attribute value belongs to the proxy and has no relationship with the original namespace.
Copy codeThe Code is as follows:
>>> Manager = multiprocessing. Manager ()
>>> Global = manager. Namespace ()
>>> Global. x = 10
>>> Global. y = 'hello'
>>> Global. _ z = 12.3 # this is an attribute of the proxy
>>> Print (Global)
Namespace (x = 10, y = 'hello ')