1. 程式人生 > >multiprocessing模組的多程序與程序池

multiprocessing模組的多程序與程序池

multiprocessing模組的Process方法

可以利用Proces方法在一個主程序中建立幾個子程序

from multiprocessing import Process
import time
def f1(name):
    time.sleep(2)
    print('Hell %s' % name)
def f2(age):
    time.sleep(2)
    print('Hell %s' % age)
if __name__ == "__main__":
    p = Process(target=f1,args=('ayu',))
    p.daemon = True #將daemon設定為True,則主程序不等待子程序,主程序結束,則整個程序結束    
    p.start()
    p = Process(target=f2,args=('22',))
    p.daemon = True
    p.start()
    print('All Done') #子程序結束後會輸出

###程序間的記憶體是不共享的

from multiprocessing import Process
li = []
def ad(i):
    li.append(i)
    print(li)
if __name__ == "__main__":
    for i in range(10):
        p = Process(target=ad,args=(i))
        p.start()
/Users/wuxiangyu-pc/.conda/envs/test_all/bin/python /Users/wuxiangyu-pc/Documents/spider/test_all/fork_process.py
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]

說明各個程序間,記憶體是不能共享的
但是執行緒之間記憶體是可以共享的,所以可以使用threading操作

from threading import Thread
li = []
def ad(i):
    li.append(i)
    print(li)
if __name__ == "__main__":
    for i in range(10):
        p = Thread(target=ad,args=(i,))
        p.start()
/Users/wuxiangyu-pc/.conda/envs/test_all/bin/python /Users/wuxiangyu-pc/Documents/spider/test_all/fork_process.py
[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 finished with exit code 0

要實現程序間的記憶體共享,可以使用Manager方法

from multiprocessing import Process,Manager
def ad(i,li):
    li.append(i)
    print(li)
if __name__ == "__main__":
    manager = Manager()
    li = manager.li()
    for i in range(10):
        p = Process(target=ad,args=(i,li))
        p.start()
        p.join()
/Users/wuxiangyu-pc/.conda/envs/test_all/bin/python /Users/wuxiangyu-pc/Documents/spider/test_all/fork_process.py
[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 finished with exit code 0

##multiprocessing模組的Pool程序池
Pool.apply方法可以實現多個子程序排序依次執行

from multiprocessing import Pool
import time
def f0(name):
    time.sleep(2)
    print('i am %s' % name)
if __name__ == "__main__":
    p = Pool(5)
    for i in range(5):
        p.apply(func=f0,args=(i,))
        print('Hello World')
    p.close()
    p.join()

Pool.apply_async實現多執行緒非同步,比apply多一個回撥函式

from multiprocessing import Pool
def f1(num):
    i = num + 20
    return i
def f1(i):
    print('i am %s' % i)
if __name__ == "__main__":
    p = Pool(5)
    for i in range(5):
        p.apply_async(func=f1,args=(i,),callback=f1)
    p.close()
    p.join()