python 多線程之Queue

來源:互聯網
上載者:User

標籤:串連   main   art   join()   通訊   range   相同   child   線程   

#進程之間的通訊# 1.Queue#跟線程裡的queue類似但是不同from multiprocessing import Process,Queueimport osdef f(q, n):    q.put([11, n, None])    print(‘subpro‘,id(q))if __name__==‘__main__‘:    q=Queue()    print(‘mainpro‘,id(q))    lst=[]    for i in range(3):        p=Process(target=f,args=(q,i))  #必須把q傳進去,因為不同進程間記憶體是不共用的        lst.append(p)        p.start()    print(q.get())    print(q.get())    print(q.get())    for i in lst:        i.join()# 主進程和其他子進程q(隊列)的記憶體位址是不同的,說明並不是相同的q,python內部進行了一些操作,# 可能是進程之間在傳遞佇列時,進行了pickle操作。
# 2.Pipesfrom multiprocessing import Process,Pipedef f(conn):    conn.send([1,‘aaa‘])    conn.close()if __name__==‘__main__‘:    # 返回父進程串連和子進程串連    parent_conn,child_conn=Pipe()    p=Process(target=f,args=(child_conn,))   #參數必須加()別忘了,還有,也不能忘    p.start()    print(parent_conn.recv())   #跟socket不同的是recv()不能加資料大小參數,                                # 傳輸資料不一定是bytes    p.join()

 

python 多線程之Queue

聯繫我們

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