Python 多進程 多線程數據共享
阿新 • • 發佈:2017-05-20
python 線程queue.queue 進程multiprocess.queue
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
from multiprocessing import Process, Queue
import os, time, random
‘‘‘
1. 我們平時from queue import Queue是線程對列,用於數據共享的,只能在線程之間進行使用;
2. from multiprocessing import Queue,是進程對列,用於進程間數據交換,實際中是在進程之間進行序列化和反序列化(pickle)
完成數據交互的;
3. 線程之間修改同一份數據,需加鎖,而進程間的數據傳遞,僅是傳遞(數據共享)。
‘‘‘
# 寫數據進程執行的代碼:
def write(q):
print(‘Process to write: %s‘ % os.getpid())
for value in [‘A‘, ‘B‘, ‘C‘]:
print(‘Put %s to queue...‘ % value)
q.put(value) # 推送
time.sleep(random.random())
# 讀數據進程執行的代碼:
def read(q):
print(‘Process to read: %s‘ % os.getpid())
while True:
value = q.get(True) # 獲取
print(‘Get %s from queue.‘ % value)
if __name__==‘__main__‘:
# 父進程創建Queue,並傳給各個子進程:
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
# 啟動子進程pw,寫入:
pw.start()
# 啟動子進程pr,讀取:
pr.start()
# 等待pw結束:
pw.join()
# pr進程裏是死循環,無法等待其結束,只能強行終止:
pr.terminate()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
from multiprocessing import Process, Pipe
from time import sleep
from multiprocessing import freeze_support
‘‘‘
def Pipe(duplex=True):
return Connection(), Connection()
說明個問題:Pipe僅能兩個進程間的交互,類似電話線形式收發
‘‘‘
def run_A(conn):
for i in range(3):
conn.send("I am from run_A: %s" % i)
sleep(0.3)
for i in range(5):
print(conn.recv())
def run_B(conn):
for i in range(3):
print(conn.recv())
for i in range(5):
conn.send("I am from run_B: %s" % i)
sleep(0.3)
if __name__ == "__main__":
freeze_support()
conn1, conn2 = Pipe()
pA = Process(target=run_A, args=(conn1,))
pB = Process(target=run_B, args=(conn2,))
pA.start()
pB.start()
pA.join()
pB.join()本文出自 “90SirDB” 博客,請務必保留此出處http://90sirdb.blog.51cto.com/8713279/1927751
Python 多進程 多線程數據共享
