Introduction to Python multiprocessing. Manager and instance (sharing data between processes), multiprocessing

Source: Internet
Author: User

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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.