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】Python中的多线程与多进程
- python 多线程多进程
- C++多线程调用Python多进程
- Python 多进程/多线程 学习笔记
- Python 多进程 多线程
- Python 多进程_进程池_多线程_线程池实现比较
- Python 多线程 多进程 GIL
- python之路-socket进阶篇,多线程、多进程
- 谈谈python的GIL、多线程、多进程
- 深入浅析python中的多进程、多线程、协程
- 浅析Python中的多进程与多线程的使用
- python多线程与多进程
- python多线程、异步、多进程+异步爬虫
- python中的多进程和多线程
- python 并发编程(多进程、多线程、gevent)
- Python学习笔记——多线程,多进程
- 【Python】[进程和线程]多进程,多线程,ThreadLocal,进程VS.线程,分布式进程
- Python 多进程开发与多线程开发
- 为什么在Python里推荐使用多进程而不是多线程?
- Python的多线程/多进程