1.管道使用
成都創(chuàng)新互聯(lián)公司服務項目包括南縣網(wǎng)站建設、南縣網(wǎng)站制作、南縣網(wǎng)頁制作以及南縣網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,南縣網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到南縣省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
'''
管道Pipe
'''
from multiprocessing import Process,Pipe
def f(conn):
conn.send('child message')#給主進程發(fā)送消息
conn.close()
if __name__=='__main__':
parent_conn,child_conn=Pipe()
p=Process(target=f,args=(child_conn,))
p.start()
print(parent_conn.recv())#主進程接收子進程消息
p.join()
2.Manage增強版pipe,可以接收字典,列表等
from multiprocessing import Process,Manager
import os
def f(d,l):#處理字典,列表函數(shù)
d[1]='1'
d['2']=2
l.append(os.getpid())
if __name__=="__main__":
manage_data=Manager()
d=manage_data.dict()
l=manage_data.list()
p_list=[]
for i in range(10):#建立10個進程并調用公共函數(shù)f
p = Process(target=f, args=(d, l))
p.start()
p_list.append(p)
for res in p_list:
res.join()
print(d)
print(l)
>>:
{1: '1', '2': 2}
[2472, 7972, 9760, 5328, 5488, 12316, 6780, 12592, 6676, 6408]
3.進程池與回調函數(shù)
from multiprocessing import Process,Pool
import time
import os
def Foo(i):
time.sleep(2)
print("in process",os.getpid())
return i+100
def Bar(arg):
print("-->exec done:",arg,os.getpid())
if __name__=='__main__':
pool=Pool(processes=2)#同時運行的進程數(shù)
print("main process",os.getpid())
for i in range(10):
pool.apply_async(func=Foo,args=(i,),callback=Bar)
pool.close()
pool.join()
>>:
main process 15704
in process 15336
in process 14796
-->exec done: 100 15704
-->exec done: 101 15704
in process 14796
in process 15336
-->exec done: 103 15704
-->exec done: 102 15704
in process 14796
in process 15336
-->exec done: 104 15704
-->exec done: 105 15704
in process 15336
in process 14796
-->exec done: 107 15704
-->exec done: 106 15704
in process 15336
in process 14796
-->exec done: 109 15704
-->exec done: 108 15704