python多进程间通信代码实例

yipeiwu_com5年前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设计】。

相关文章

Python2与python3中 for 循环语句基础与实例分析

Python2与python3中 for 循环语句基础与实例分析

下面的代码中python2与python3的print使用区别,大家注意一下。python3需要加()才行。 语法: for循环的语法格式如下: for iterating_var...

Python 调用Java实例详解

Python 调用Java实例详解 前言: Python 对服务器端编程不如Java 所以这方面可能要调用Java代码 前提: Linux 环境  1 安装 jpype1 安...

TensorFlow 合并/连接数组的方法

如下所示: import tensorflow as tf a = tf.Variable([4,5,6]) b = tf.Variable([1,2,3]) c = tf.co...

Python实现判断并移除列表指定位置元素的方法

Python实现判断并移除列表指定位置元素的方法

本文实例讲述了Python实现判断并移除列表指定位置元素的方法。分享给大家供大家参考,具体如下: 问题很简单,输入一个列表和索引,若索引超出列表范围则返回源列表,否则删除指定索引位置的元...

基于anaconda下强大的conda命令介绍

我们来探究window下面,使用conda命令来操作anaconda,安装库。 conda -V 检验是否安装;以及当前的anaconda版本 conda list 查看安装了那些库 c...