最近有个小课题,需要用到双进程,翻了些资料,还算圆满完成任务。记录一下~
1.简单地双进程启动
同时的调用print1()和print2()两个打印函数,代码如下:
#/usr/bin/python import multiprocessing import time run = 1 def print1(): while run == 1: print 'print 1' time.sleep(1) def print2(): while run == 1: print 'print 2' time.sleep(1) if __name__ == '__main__': p1 = multiprocessing.Process(target = print1,) p2 = multiprocessing.Process(target = print2,) p1.start() p2.start()
结果:
print 1 print 2 print 1 print 2 ...
在课题中遇到了进程p2要使用进程p1中数据的问题,考虑使用IPC的方法来处理,首先考虑了multiprocessing包里地Pipe类来实现。
2.Pipe
Pipe可以是单向(half-duplex),也可以是双向(duplex)。通过multiprocessing.Pipe(duplex = False) 创建单向管道(默认为双向)。一个进程从Pipe一段输入对象,然后被Pipe另一端的进程接收,单向管道只允许管道一端的进程输入,而双向管道则允许从两端输入。
单向传输,代码如下:
#/usr/bin/python import multiprocessing import time run = 1 def print1(pipe): num = 0 while run == 1: print 'print 1:' pipe.send(str(num)) num = num + 1 time.sleep(1) def print2(pipe): while run == 1: print 'print 2:'+pipe.recv() time.sleep(2) if __name__ == '__main__': pipe = multiprocessing.Pipe(duplex=False) p1 = multiprocessing.Process(target = print1,args = (pipe[1],)) p2 = multiprocessing.Process(target = print2,args = (pipe[0],)) p1.start() p2.start()
结果:
print 1: print 2:0 print 1: print 1: print 2:1 print 1: print 1: print 2:2 print 1: print 1: print 2:3 print 1: print 1: print 2:4
双向传输,代码如下:
#/usr/bin/python import multiprocessing import time run = 1 def print1(pipe): while run == 1: pipe.send('1') print 'print 1:'+pipe.recv() time.sleep(1) def print2(pipe): while run == 1: pipe.send('2') print 'print 2:'+pipe.recv() time.sleep(1) if __name__ == '__main__': pipe = multiprocessing.Pipe(duplex=True) p1 = multiprocessing.Process(target = print1,args = (pipe[0],)) p2 = multiprocessing.Process(target = print2,args = (pipe[1],)) p1.start() p2.start()
结果:
print 2:1 print 1:2 print 2:1 print 1:2
3.共享内存
因为课题中,两个进程的循环时间不同,接收进程慢于发送进程,使用双进程的话,会造成进程等待,而单进程的话,接收进程是从队列中按顺序取数据,不能获取实时更新数据,所以考虑使用资源共享解决这一问题。
#/usr/bin/python import multiprocessing import time run = 1 def print1(data,arr): while run == 1: print 'print 1:' data.value = data.value + 1 arr[0] = data.value time.sleep(1) def print2(data,arr): while run == 1: print 'print 2:'+str(data.value) print arr[:] time.sleep(2) if __name__ == '__main__': data = multiprocessing.Value('i',0) #整数 arr = multiprocessing.Array('d',range(10)) #列表 p1 = multiprocessing.Process(target = print1,args = (data,arr)) p2 = multiprocessing.Process(target = print2,args = (data,arr)) p1.start() p2.start()
结果:
print 2:0 [print 1: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] print 1: print 2:2 [2.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] print 1: print 1: print 2:4 [4.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] print 1: print 1: print 2:6 [6.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] ...