A brief introduction to multiple processes in python

Source: Internet
Author: User
The multiprocessing module is one of the most advanced and powerful modules in the python library. This article will give you a brief introduction to the multiprocessing general skill. The multiprocessing module is one of the most advanced and powerful modules in the python library. This article will give you a brief introduction to general multiprocessing skills

Processes are managed by the system.

1: The most basic writing method

from multiprocessing import Pooldef f(x):  return x*xif __name__ == '__main__':  p = Pool(5)  print(p.map(f, [1, 2, 3]))[1, 4, 9]

2. the process is actually generated using the OS. fork method.

In unix, all processes are generated using the fork method.

multiprocessing Processosinfo(title):  title  , __name__  (os, ): , os.getppid()  , os.getpid()f(name):  info()  , name__name__ == :  info()  p = Process(=f, =(,))  p.start()  p.join()

3. thread shared memory

threadingrun(info_list,n):  info_list.append(n)  info_list__name__ == :  info=[]  i ():    p=threading.Thread(=run,=[info,i])    p.start()[0][0, 1][0, 1, 2][0, 1, 2, 3][0, 1, 2, 3, 4][0, 1, 2, 3, 4, 5][0, 1, 2, 3, 4, 5, 6][0, 1, 2, 3, 4, 5, 6, 7][0, 1, 2, 3, 4, 5, 6, 7, 8][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Process does not share memory:

multiprocessing Processrun(info_list,n):  info_list.append(n)  info_list__name__ == :  info=[]  i ():    p=Process(=run,=[info,i])    p.start()[1][2][3][0][4][5][6][7][8][9]

To share the memory, use the Queue in the multiprocessing module.

multiprocessing Process, Queuef(q,n):  q.put([n,])__name__ == :  q=Queue()  i ():    p=Process(=f,=(q,i))    p.start()  :    q.get()

4. Lock: it is only for screen sharing. because the process is independent, it is useless for multiple processes.

multiprocessing Process, Lockf(l, i):  l.acquire()  , i  l.release()__name__ == :  lock = Lock()  num ():    Process(=f, =(lock, num)).start()hello world 0hello world 1hello world 2hello world 3hello world 4hello world 5hello world 6hello world 7hello world 8hello world 9

5. memory sharing between processes: Value, Array

multiprocessing Process, Value, Arrayf(n, a):  n.value = i ((a)):    a[i] = -a[i]__name__ == :  num = Value(, )  arr = Array(, ())  num.value  arr[:]  p = Process(=f, =(num, arr))  p.start()  p.join()0.0[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]3.1415927[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

# Manager sharing method, but slow

Multiprocessing Process, Managerf (d, l): d [] = d [] = d [] = l. reverse () _ name _ =: manager = Manager () d = manager. dict () l = manager. list () p = Process (= f, = (d, l) p. start () p. join () d l # print '-------------' is another method # print pool. map (f, range (10) {0.25: None, 1: '1', '2': 2} [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# Asynchronous: This method is rarely used

Multiprocessing Pooltimef (x): x * x time. sleep () x * x _ name _ =: pool = Pool (=) res_list = [] I (): res = pool. apply_async (f, [I]) res_list.append (res) r res_list: r. get (timeout = 10) # timeout

Apply is used for synchronization.

For more information about the multi-process related articles in python, please follow the PHP Chinese network!

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.