python多进程间通信代码实例

yipeiwu_com6年前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使用urllib模块和pyquery实现阿里巴巴排名查询

urllib基础模块的应用,通过该类获取到url中的html文档信息,内部可以重写代理的获取方法 复制代码 代码如下:class ProxyScrapy(object): &n...

Python3 执行Linux Bash命令的方法

和之前C++执行Linux Bash命令的方法 一样,Python依然支持system调用和popen()函数来执行linux bash命令。 方法一:system调用 #仅仅在一个...

Pandas:Series和DataFrame删除指定轴上数据的方法

如下所示: import numpy as np import pandas as pd from pandas import Series,DataFrame 一、drop方法:...

Python中logging.NullHandler 的使用教程

在使用 peewee 框架时,默认是不会出现日志消息的。 from peewee import Model, CharField, DateTimeField, IntegerFie...

Python中的元类编程入门指引

回顾面向对象编程 让我们先用 30 秒钟来回顾一下 OOP 到底是什么。在面向对象编程语言中,可以定义 类,它们的用途是将相关的数据和行为捆绑在一起。这些类可以继承其 父类的部分或全部性...