python-多進程

來源:互聯網
上載者:User

標籤:python

進程是由系統自己管理的。

1:最基本的寫法

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、實際上是通過os.fork的方法產生進程的

unix中,所有進程都是通過fork的方法產生的。

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


3、線程共用記憶體

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]

進程不共用記憶體:

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]


若想共用記憶體,需使用multiprocessing模組中的Queue

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


4、鎖:僅是對於螢幕的共用,因為進程是獨立的,所以對於多進程沒有用

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、進程間記憶體共用: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共用方法,但速度慢

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 ‘-------------‘這裡只是另一種寫法# print pool.map(f,range(10))
{0.25: None, 1: ‘1‘, ‘2‘: 2}[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]


#非同步:這種寫法用的不多

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) #逾時時間


01491625360149464981162536496481

同步的就是apply








本文出自 “行者” 部落格,請務必保留此出處http://223228686.blog.51cto.com/2222284/1869862

python-多進程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.