Multi-process multiprocessing

Source: Internet
Author: User

1-

What is the comparison between multiprocessing and threading?

Multi-process multiprocessing and multithreaded threading are similar, they are used in Python for parallel operations. But now that we have threading, why does Python have a multiprocessing? The reason is simple enough to compensate for some of threading's weaknesses, such as the Gil mentioned in the threading tutorial.

The use of multiprocessing is also very simple, if you have a certain understanding of threading friends, your enjoyment of the time is up. Because Python uses multiprocessing and threading in almost the same way. This makes it easier for us to get started. It's also easier to unleash the power of your computer's multicore system!

2-

Add process import thread process standard module
Import multiprocessing as Mpimport threading as TD

  

Defines a function that is called by threads and processes
def job (a,d):    print (' AAAAA ')

  

Creating Threads and processes
T1 = TD. Thread (target=job,args=) P1 = MP. Process (target=job,args=)

  

Note: The first letter of the thread and process are capitalized, the function being called has no parentheses, and the parameters of the called function are placed in the args (...). In

Starting threads and processes separately

T1.start () P1.start ()

connecting threads and processes individually

T1.join () P1.join ()

  

Full thread and process creation using contrast code
Import multiprocessing as Mpimport threading as Tddef Job (a,d):    print (' aaaaa ') T1 = TD. Thread (target=job,args=) P1 = MP. Process (target=job,args=) T1.start () P1.start () T1.join () P1.join ()

  

Use
If __name__== ' __main__ ':

Full application code:

Import multiprocessing as Mpdef job (a,d):    print (' aaaaa ') if __name__== ' __main__ ':    p1 = MP. Process (target=job,args=)    P1.start ()    p1.join ()

Operating environment in the terminal environment, it is possible that other editing tools will appear without printing results after the run finishes, and the result of printing in terminal is:

Aaaaa

  

#3-Queue
# View More Python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial:https://www.youtube.com/channel/ucdyjib5h8pu7adtnvxttpcg# Youku video tutorial:http:// I.youku.com/pythontutorialimport multiprocessing as Mpdef job (q): res = 0 for i in range: res + = I+i**2 +i**3 Q.put (res) # Queueif __name__ = = ' __main__ ': q = MP. Queue () P1 = MP. Process (Target=job, args= (Q,)) P2 = MP. Process (Target=job, args= (Q,)) P1.start () P2.start () p1.join () p2.join ( ) res1 = Q.get () res2 = Q.get () print (res1+res2)

  

#4-comparison# View More Python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial:https://www.youtube.com/channel/ucdyjib5h8pu7adtnvxttpcg# Youku video tutorial:http:// I.youku.com/pythontutorialimport multiprocessing as Mpimport threading as Tdimport timedef Job (q): res = 0 for i in Range (1000000): res + = i+i**2+i**3 Q.put (res) # Queuedef multicore (): Q = MP. Queue () P1 = MP. Process (Target=job, args= (q,)) P2 = MP.    Process (Target=job, args= (Q,)) P1.start () P2.start () P1.join () p2.join () Res1 = Q.get () Res2 = Q.get ()            Print (' multicore: ', res1+res2) def normal (): res = 0 for _ and Range (2): For I in Range (1000000): Res + = i+i**2+i**3 print (' Normal: ', res) def multithread (): Q = MP. Queue () T1 = TD. Thread (Target=job, args= (q,)) t2 = TD.    Thread (Target=job, args= (Q,)) T1.start () T2.start () T1.join () t2.join () Res1 = Q.get () Res2 = Q.get () Print (' multithread: ', res1+res2) if __name__ = = ' __main__ ': st = Time.time () normal () st1= time.time () print (' Normal time: ', st1-st) multithread () St2 = Time.time () print (' multithread time: ', St2-st1) multicore () print (' multicore time: ', Time.time ()-st2)
#5-Pool# View More Python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial:https://www.youtube.com/channel/ucdyjib5h8pu7adtnvxttpcg# Youku video tutorial:http:// I.youku.com/pythontutorialimport multiprocessing as Mpdef job (x):    return X*xdef multicore ():    pool = MP. Pool (processes=2)    res = Pool.map (job, Range)    print (res)    res = Pool.apply_async (Job, (2))    print ( Res.get ())    multi_res =[pool.apply_async (Job, (I,)) for I in range ()    print ([Res.get () for res in multi_res]) if __name__ = = ' __main__ ':    multicore ()

6-Shared memory

Shared Value

We can store it Value in a shared memory table by using the data.

 

Import multiprocessing as Mpvalue1 = MP. Value (' I ', 0) value2 = MP. Value (' d ', 3.14)

where d and i parameters are used to set the data type, d representing a double-precision floating-point type that i represents a signed integer. For more details, please see the table at the end of this page.

Shared Array

In Python, there is mutiprocessing also a Array class that can interact with shared memory to enable sharing of data between processes.

Array = MP. Array (' i ', [1, 2, 3, 4])

  

Here Array and NumPy in the difference, it can only be one-dimensional, can not be multidimensional. Similarly Value , you need to define the data form, otherwise you will get an error. We will give an example of how these two are used in the latter section.

Wrong form

Array = MP. Array (' i ', [[1, 2], [3, 4]]) # 2-dimensional list "" "Typeerror:an Integer is required" ""

  



Data types represented by each parameter
| Type Code | C Type | Python Type | Minimum size in bytes | | --------- | ------------------ | ----------------- | --------------------- || ' B ' | Signed Char | int | 1 | | ' B ' | unsigned char | int | 1 | | ' U ' | Py_unicode | Unicode Character | 2 | | ' H ' | Signed Short | int | 2 | | ' H ' | unsigned short | int | 2 | | ' I ' | Signed int | int | 2 | | ' I ' | unsigned int | int | 2 | | ' L ' | Signed Long | int | 4 | | ' L ' | unsigned long | int | 4 | | "' Q ' | Signed Long Long | int | 8 | | "' Q ' | unsigned Long Long | int |        8             || ' F ' | float | float | 4 | | ' d ' | Double | float | 8 |

 

#7-lock# View More Python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial:https://www.youtube.com/channel/ucdyjib5h8pu7adtnvxttpcg# Youku video tutorial:http:// I.youku.com/pythontutorialimport multiprocessing as Mpimport timedef job (V, NUM, l):    L.acquire () for    _ in range ( ):        Time.sleep (0.1)        v.value + = num        print (v.value)    l.release () def multicore ():    L = MP. Lock ()    v = MP. Value (' I ', 0)    P1 = MP. Process (Target=job, args= (V, 1, L))    P2 = MP. Process (Target=job, args= (V, 3, L))    P1.start ()    P2.start ()    p1.join ()    p2.join () if __name__ = = ' __ Main__ ':    multicore ()

  

 

  

Multi-process multiprocessing

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.