python进程通信的几种实现方式
from multiprocessing import Pipe,Process,Queue,Manager,JoinableQueue
1、管道
#管道 def task(name,a): a.send({name:[i for i in range(10000)]}) print('当前进程号%s'%os.getpid() + '父进程号%s'%os.getppid()) if __name__ == '__main__': child_conn,parent_conn = Pipe() job = [] task_values = [] for i in range(5): p = Process(target=task,args=(i,child_conn)) job.append(p) p.start() data = parent_conn.recv() task_values.append(data) for i in job: i.join() print(task_values) print('当前进程号%s' % os.getpid() + '父进程号%s' % os.getppid())
2、queue队列
#queue队列 def task1(queue): task_values = [] while True: res = queue.get() task_values.append(res) if res is None: print(task_values) break # 收到结束信号则结束 def task(name,queue): queue.put({name: [i for i in range(10000)]}) print('当前进程号%s' % os.getpid() + '父进程号%s' % os.getppid()) if __name__ == '__main__': queue = Queue() jobs = [] c = Process(target=task1,args=(queue,)) c.start() for i in range(5): p = Process(target=task, args=(i, queue)) jobs.append(p) p.start() for job in jobs: job.join() queue.put(None)
3、数据共享
#数据共享 def task(name,manager): manager[name] = {name:[i for i in range(10000)]} print('当前进程号%s' % os.getpid() + '父进程号%s' % os.getppid()) if __name__ == '__main__': manager = Manager() m = manager.list() jobs = [] for i in range(5): m.append(i) p = Process(target=task, args=(i, m)) jobs.append(p) p.start() for job in jobs: job.join() print(m)
4、JoinableQueue队列
#JoinableQueue队列 def producer(name,jq): jq.put({name:[i for i in range(10000)]}) print('当前进程号%s' % os.getpid() + '父进程号%s' % os.getppid()) def consumer(jq): task_values = [] while True: res = jq.get() task_values.append(res) if res is None: print(task_values) jq.task_done() if __name__ == '__main__': jq = JoinableQueue() c = Process(target=consumer,args=(jq,)) c.daemon = True c.start() jobs = [] for i in range(5): p = Process(target=producer, args=(i, jq)) jobs.append(p) p.start() for job in jobs: job.join() jq.put(None) jq.join() print('主进程结束')
刷初步设计被拆包时间戳