【進程與線程】多進程

來源:互聯網
上載者:User

標籤:main   tip   col   __name__   queue   finally   fun   一個   進程同步   

多進程multiprocessing

from multiprocessing import Processimport osdef info(title):    print(title)    print(‘module name:‘,__name__)    print(‘parent process:‘,os.getppid())    print(‘process id:‘,os.getpid())    print(‘\n‘)def f(name):    print(‘\033[31;1mfunction f\033[0m‘)    print(‘hello‘,name)if __name__ == ‘__main__‘:    info(‘\033[32;1mmain process line\033[0m‘)    p=Process(target=f,args=(‘q1ang‘,))    p.start()    # p.join()

>>>

main process linemodule name: __main__parent process: 8044process id: 11048function fhello q1ang

兩進程間的通訊

不同進程間記憶體是不共用的,要想實現兩個進程間的資料交換,可以用以下方法:

1.進程Queues

def a(q):    q.put(‘hehe‘)if __name__==‘__main__‘:    q=Queue()    p=Process(target=a,args=(q,))    p.start()    print(q.get())    p.join()

2.Pipe

from multiprocessing import Process,Pipedef a(conn):    conn.send(‘hehe‘)    conn.close()if __name__ == ‘__main__‘:    parent_conn,child_conn=Pipe()    p=Process(target=a,args=(child_conn,))    p.start()    print(parent_conn.recv())    p.join()

3.Managers

from multiprocessing import Process,Managerdef a(d,l):    d[1]=‘1‘    d[‘2‘]=2    d[3]=None    l.append(1)    print(l)if __name__ == ‘__main__‘:    with Manager() as manager:        d=manager.dict()        l=manager.list(range(5))        p_list=[]        for i in range(10):            p=Process(target=a,args=(d,l))            p.start()            p_list.append(p)        for res in p_list:            res.join()        print(d)        print(l)

 進程同步:主要功能是螢幕鎖

from multiprocessing import Process,Lockdef f(l,i):    l.acquire()    try:        print(‘hello world‘,i)    finally:        l.release()if __name__ == ‘__main__‘:    lock=Lock()    for num in range(10):        Process(target=f,args=(lock,num)).start()

進程池

進程池內部維護一個進程式列,當使用時,則去進程池中擷取一個進程,如果進程池序列中沒有可供使用的進進程,那麼程式就會等待,直到進程池中有可用進程為止。

apply_async(並行)
apply(串列)

from multiprocessing import Poolimport time,osdef Foo(i):    time.sleep(1)    print(i)    return idef Bar(arg):    print(‘-->exec done:‘, arg,os.getpid())if __name__ == ‘__main__‘:    pool = Pool(5) #允許進程池同時放入五個進程    print(os.getpid())    for i in range(10):        #apply_async(並行)        #apply(串列)        # pool.apply_async(func=Foo, args=(i,), callback=Bar)#callback回調        pool.apply(func=Foo, args=(i,))        print(‘end‘)    pool.close()    pool.join()  # 進程池中進程執行完畢後再關閉,如果注釋,那麼程式直接關閉。

 

【進程與線程】多進程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.