python如何实现多进程间通信功能

这篇文章给大家分享的是有关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()

执行后的效果:

python如何实现多进程间通信功能

感谢各位的阅读!关于“python如何实现多进程间通信功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!