multiprocessing

1-

What is Multiprocessing

Comparison with threading 

Multiprocessing Multiprocessing is similar to multithreading. They are used for parallel computing in python. But since there is threading, why does Python have a multiprocessing? The reason is very simple, it is used to make up for some disadvantages of threading, such as The GIL mentioned in the threading  tutorial .

Using multiprocessing is also very simple. If you have a certain understanding of threading, your enjoyment time is up. Because python uses multiprocessing and threading in almost the same way. This makes it easier for us to get started. It is also easier to use the multi-core of your computer The power of the system!

2-

Add process Process

Import the thread process standard module

import multiprocessing as mp
import threading as td

  

Define a function to be called by threads and processes

def job(a,d):
    print('aaaaa')

  

Create threads and processes

t1 = td.Thread(target=job,args=(1,2))
p1 = mp.Process(target=job,args=(1,2))

  

Note: The first letter of Thread and Process must be capitalized, the called function has no parentheses, and the parameters of the called function are placed in args(…)

Start threads and processes separately

t1.start()
p1.start()

  Connect threads and processes separately

t1.join()
p1.join()

  

Complete thread and process creation using comparison code

import multiprocessing as mp
import threading as td

def job(a,d):
    print('aaaaa')

t1 = td.Thread(target=job,args=(1,2))
p1 = mp.Process(target=job,args=(1,2))
t1.start()
p1.start()
t1.join()
p1.join()

  

use

if __name__=='__main__':

  Complete application code:

import multiprocessing as mp

def job(a,d):
    print('aaaaa')

if __name__=='__main__':
    p1 = mp.Process(target=job,args=(1,2))
    p1.start()
    p1.join()

  The running environment should be in the terminal environment, other editing tools may not print the result after the running, and the printed result after running in the 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/pythontutorial import multiprocessing as mp def job(q): res = 0 for i in range(1000): res += i+i**2+i**3 q.put(res) # queue if __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/pythontutorial

import multiprocessing as mp
import threading as td
import time

def job(q):
    res = 0
    for i in range(1000000):
        res += i+i**2+i**3
    q.put(res) # queue

def 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 _ in 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/pythontutorial

import multiprocessing as mp

def job(x):
    return x*x

def multicore():
    pool = mp.Pool(processes=2)
    res = pool.map(job, range(10))
    print(res)
    res = pool.apply_async(job, (2,))
    print(res.get())
    multi_res =[pool.apply_async(job, (i,)) for i in range(10)]
    print([res.get() for res in multi_res])

if __name__ == '__main__':
    multicore()

 6- Shared memory

Shared Value

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

 

import multiprocessing as mp

value1 = mp.Value('i', 0)
value2 = mp.Value('d', 3.14)

  The dsum iparameter is used to set the data type, which dmeans a double precision floating point type, which imeans a signed integer type. For more forms please see the table at the end of this page.

Shared Array 

In Python mutiprocessing, there is also a Arrayclass that can interact with shared memory to share data between processes.

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

  

The one here is Arraydifferent from that in numpy, it can only be one-dimensional, not multi-dimensional. The same Value as the same, the data format needs to be defined, otherwise an error will be reported. We will illustrate the use of these two methods in the next section.

wrong form

array = mp.Array('i', [[1, 2], [3, 4]]) # 2维list

"""
TypeError: an integer is required
"""

  



The data type 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/pythontutorial

import multiprocessing as mp
import time

def job(v, num, l):
    l.acquire()
    for _ in range(10):
        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()

  

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127697&siteId=291194637