python多进程间通信

这里使用pipe代码如下:

import time
from multiprocessing import Process
import multiprocessing

class D:
    @staticmethod
    def test(pipe):
        while True:
            for i in range(10):
               pipe.send(i)
              time.sleep(2)

    @staticmethod
    def test2(pipe):
        while True:
       print('test2 value:%s' % pipe.recv()) time.sleep(2) if __name__ == '__main__': pipe = multiprocessing.Pipe() p = Process(target=D.test2,args=(pipe[0],)) p2 = Process(target=D.test,args=(pipe[1],)) p.start() p2.start()

 

执行后的效果:

 

posted @ 2019-09-20 01:22  科技改变未来☆  阅读(173)  评论(0编辑  收藏  举报