Python learning-eight weeks two lessons (December 12)

Source: Internet
Author: User

Tag: Use GIF to perform service ROC range based on function src

Eight weeks two sessions (December 12)
14.6 Multi-Process Manager
14.7 Process Pool

Manager

The manager object is similar to the communication between the server and the customer (server-client), similar to our activity on the Internet. We use a process as a server to build the manager to really store the resources. Other processes can be passed through parameters or access the manager based on the address, and after establishing the connection, manipulate the resources on the server. With the firewall permitting, we can fully apply the manager to multiple computers, mimicking a real-world network scenario. In the following example, our use of the manager is similar to shared memory, but we can share a richer object type.

Import Multiprocessingdef f (x, arr, L):    x.value = 3.14    arr[0] = 5    l.append (' Hello ') server = multiprocessing. Manager () x    = server. Value (' d ', 0.0) arr  = server. Array (' I ', Range (Ten)) L    = server.list () proc = multiprocessing. Process (Target=f, args= (x, arr, L)) Proc.start () Proc.join () print (x.value) print (arr) print (L)

The manager uses the list () method to provide a way to share tables. You can actually use Dict () to share the dictionary, Lock () to share the threading. Lock (Note that we are sharing the threading. Lock, not the mutiprocessing of the process. Lock. The latter itself has realized process sharing) and so on. This allows the manager to allow us to share more diverse objects

The process pool can create multiple processes.

For example, the following program:

Import multiprocessing as Muldef F (x):    return x**2pool = Mul. Pool (5) rel = Pool.map (f,[1,2,3,4,5,6,7,8,9,10])
Print (REL)

We have created a process pool that allows 5 processes. Each process running by the pool executes the f () function. We use the map () method to effect the F () function on each element of the table. This is similar to built-in's map () function, except that it is handled in parallel with 5 processes. If there are elements that need to be processed after the process has finished running, then the process is used to rerun the F () function. In addition to the map () method, the pool has the following common methods.

Apply_async (Func,args) takes a process from the process pool to execute the parameter Func,args to Func. It returns an AsyncResult object that you can call the Get () method to get the result.

The close () process pool no longer creates a new process

Join () Wait for all processes in the process pool. You must first call the close () method on the pool to join.

Python learning-eight weeks two lessons (December 12)

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.