老男孩python学习自修第二十四天【多进程】

1. 体验多进程的运行速度

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from multiprocessing import Pool
import time

def foo(n):
    time.sleep(1)
    return n * n

if __name__ == "__main__":
    pool = Pool(10)
    data_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    # 这里只需要等待1S就能得到结果, 因为使用了多进程
    print pool.map(foo, data_list)
    # 这里需要等待10S 才能得到结果, 因为没有使用多进程
    print map(foo, data_list)

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day24/processing_test.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Process finished with exit code 0

注意:

(1)多进程的优势在于充分利用多核优势,因为多线程都是利用单核,只有多进程才能利用多核

2.子进程与父进程的关系

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from multiprocessing import Process
import os

def info(title):
    print "+++++++++++++" + title + "++++++++++++++"
    if hasattr(os, 'getppid'):
        print "ppid: %d" % os.getppid()
    print "pid: %d" % os.getpid()

if __name__ == "__main__":
    info("main process")

    process = Process(target=info, args=("other process", ))
    process.start()
    process.join()

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day24/processing_test_2.py
+++++++++++++main process++++++++++++++
ppid: 490
pid: 593
+++++++++++++other process++++++++++++++
ppid: 593
pid: 594

Process finished with exit code 0

3.进程与线程的区别

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from multiprocessing import Process
from threading import Thread

def foo(li, item):
    li.append(item)
    print li

if __name__ == "__main__":
    li = []

    #注意:进程是不共享内存的,每个进程都有独立的内存空间;可以利用多核优势;是真正意义上的并发
    print "+++++++++process+++++++++"
    for item in range(5):
        process = Process(target=foo, args=(li, item))
        process.start()
        process.join()

    #注意:线程是共享同一份内存的,每个线程都在抢占内存空间;一个时间片只有一个线程占用内存;不是真正的并发
    print "+++++++++thread++++++++++"
    for item in range(5):
        thread = Thread(target=foo, args=(li, item))
        thread.start()
        thread.join()

结果:

+++++++++process+++++++++
[0]
[1]
[2]
[3]
[4]
+++++++++thread++++++++++
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]

4. 实现进程间的内存共享(使用进程的队列)

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from multiprocessing import Process, Queue

def foo(que, item):
    que.put(item)

if __name__ == "__main__":
    que = Queue()
    for item in range(5):
        process = Process(target=foo, args=(que, item))
        process.start()
    while True:
        print que.get()

结果:

1
2
0
3
4

5. 实现进程间的内存共享(使用Value和Array)

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from multiprocessing import Process, Value, Array

def foo(n, a):
    n.value = 3.1415926
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == "__main__":

    # 这里采用多进程提供的Value对象和Array对象
    num = Value('d', 0)
    arr = Array('i', range(10))

    # 调用foo方法,修改子进程的内存数据
    process = Process(target=foo, args=(num, arr))
    process.start()
    process.join()

    # 打印主进程定义的使用Value和Array定义的数据,发现也被修改;从而断定子进程与主进程共享了内存空间
    print num.value
    print arr[:]

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day24/processing_test_5.py
3.1415926
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

Process finished with exit code 0

6. 使用进程池开启进程

#!/usr/bin/env python
#! _*_ coding:UTF-8 _*_

from multiprocessing import Pool
import time

def foo(x):
    print x * x
    time.sleep(1)
    return x * x

if __name__ == "__main__":

    # 这里定义进程池,每次最多只有4个进程并行运行
    pool = Pool(processes=4)

    res_list = []
    for i in range(10):
        # 开启新的进程并启动,相当于Process(target=foo, args=(i))
        res = pool.apply_async(foo, (i, ))
        # 要将进程的运行结果放入列表中,这时其实进程还还有执行函数foo
        res_list.append(res)

    for item in res_list:
        # 只有在区结果的时候,进程才真正执行函数
        print item.get()

结果:看见每个时间段只有4个进程运行

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day24/processing_test_6.py
0
1
4
9
16
25
36
0
1
49
4
9
64
81
16
25
36
49
64
81

Process finished with exit code 0
上一篇:BI案例:BI在连锁零售业应用(ZT)


下一篇:数据流重定向和管道命令, grep, tr,sort, wc, cut,split,tee,sleep(shell 02)