新聞中心
進程queue傳遞
frommultiprocessingimportProcess,Queue
deff(qq):
qq.put([42,None,'hello'])
if__name__ =='__main__':
q = Queue()#父進程queue
p = Process(target=f,args=(q,))#子進程 父進程的queue傳給子進程 實現(xiàn)數(shù)據(jù)傳遞
p.start()#啟動子進程
print(q.get()) # prints "[42, None, 'hello']"父進程q可以get到子進程p實現(xiàn)父進程子進程數(shù)據(jù)共享
p.join()
#線程queue不能傳給進程
為留壩等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及留壩網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站建設(shè)、留壩網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
管道傳遞
frommultiprocessingimportProcess, Pipe
deff(conn):
conn.send([42,None,'hello from child'])
conn.send([43,None,'hello from child2'])
print("child recv:",conn.recv())
conn.close()
if__name__ =='__main__':
parent_conn, child_conn = Pipe() #生成管道實例,取出兩端
p = Process(target=f,args=(child_conn,))
p.start()
print(parent_conn.recv())# prints "[42, None, 'hello']"
print(parent_conn.recv())
parent_conn.send("hello send by parent")
p.join()
manager數(shù)據(jù)共享
frommultiprocessing importProcess, Manager
importos
deff(d, l):#每個子進程執(zhí)行的函數(shù)
d[os.getpid()]=os.getpid()
l.append(os.getpid())
print(l)
if__name__ =='__main__':
withManager()asmanager:#
d = manager.dict()#父進程生成一個字典 多個進程之間可共享的字典
l = manager.list(range(5))#父進程生成一個列表 多個進程之間可以共享的列表 默認有5個數(shù)據(jù)
p_list = []#用于多個進程join
foriinrange(10):
p = Process(target=f,args=(d, l))
p.start()#開啟10個子進程
p_list.append(p)
forresinp_list:#等待每個子進程結(jié)果
res.join()
print(d)
print(l)
名稱欄目:多進程_父進程子進程數(shù)據(jù)傳遞共享
分享網(wǎng)址:http://www.dlmjj.cn/article/piisdh.html