您的位置:首页 > 编程语言 > Python开发

Python 多进程 多线程数据共享

2017-05-20 10:48 253 查看
#!/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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python queue 线程