美文网首页
使用os pipe管道使python fork多进程之间通信

使用os pipe管道使python fork多进程之间通信

作者: 皮儿吃屁 | 来源:发表于2020-12-27 21:10 被阅读0次

管道(pipe)

管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信;

实现机制:

管道是由内核管理的一个缓冲区,相当于我们放入内存中的一个纸条。管道的一端连接一个进程的输出。这个进程会向管道中放入信息。管道的另一端连接一个进程的输入,这个进程取出被放入管道的信息。一个缓冲区不需要很大,它被设计成为环形的数据结构,以便管道可以被循环利用。当管道中没有信息的话,从管道中读取的进程会等待,直到另一端的进程放入信息。当管道被放满信息的时候,尝试放入信息的进程会等待,直到另一端的进程取出信息。当两个进程都终结的时候,管道也自动消失。


pipe

从原理上,管道利用fork机制建立,从而让两个进程可以连接到同一个PIPE上。最开始的时候,上面的两个箭头都连接在同一个进程Process 1上(连接在Process 1上的两个箭头)。当fork复制进程的时候,会将这两个连接也复制到新的进程(Process 2)。随后,每个进程关闭自己不需要的一个连接 (两个黑色的箭头被关闭; Process 1关闭从PIPE来的输入连接,Process 2关闭输出到PIPE的连接),这样,剩下的红色连接就构成了如上图的PIPE。

fork
import os, time, sys
pipe_name = 'pipe_test'

def child( ):
    pipeout = os.open(pipe_name, os.O_WRONLY)
    counter = 0
    while True:
        time.sleep(1)
        os.write(pipeout, 'Number %03d\n' % counter)
        counter = (counter+1) % 5

def parent( ):
    pipein = open(pipe_name, 'r')
    while True:
        line = pipein.readline()[:-1]
        print 'Parent %d got "%s" at %s' % (os.getpid(), line, time.time( ))

if not os.path.exists(pipe_name):
    os.mkfifo(pipe_name)  
pid = os.fork()    
if pid != 0:
    parent()
else:       
    child()

[xiaorui@localhost ~ ]$ python f2.py
Parent 17355 got "Number 000" at 1418177682.7
Parent 17355 got "Number 001" at 1418177683.7
Parent 17355 got "Number 002" at 1418177684.7
Parent 17355 got "Number 003" at 1418177685.7
Parent 17355 got "Number 004" at 1418177686.7
Parent 17355 got "Number 000" at 1418177687.7
Parent 17355 got "Number 001" at 1418177688.7
Parent 17355 got "Number 002" at 1418177689.7
Parent 17355 got "Number 003" at 1418177690.71
Parent 17355 got "Number 004" at 1418177691.71
Parent 17355 got "Number 000" at 1418177692.71
Parent 17355 got "Number 001" at 1418177693.71
Parent 17355 got "Number 002" at 1418177694.71
Parent 17355 got "Number 003" at 1418177695.71

原文地址:http://xiaorui.cc/archives/795

相关文章

  • 使用os pipe管道使python fork多进程之间通信

    管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功...

  • Python多进程

    Python多进程 Python中进程创建方式有两种: fork:使用Python提供的os模块。 Python在...

  • Linux 进程之间的通信方式

    linux使用的进程间通信方式 管道(pipe)、流管道(s_pipe)、无名管道(FIFO)、 套接字 sock...

  • Linux-C-day-2-进程通过--管道通信

    管道通信 进程间管道通信方式可以通过man 7 pipe来查看; 匿名管道 单工管道 打开管道:使用popen()...

  • Android系统之Binder通信机制

    前言 在Linunx进程中使用的通信方式有:socket(套接字通信),named(命令管道),pipe(管道),...

  • python 的进程、线程以及协程(1)

    python 的进程、线程以及协程(1) python实现多进程主要有两种,一种是os模块的fork方法,另一种是...

  • 第九节 netty前传-NIO pipe

    pipe管道作为线程之间通信的一种方式 首先作为对比我们先了解下再BIO模式下的pipe的使用Pipe为运行在同一...

  • 多进程笔记:

    ### 在Python中多进程的创建方式对比: 1. 在Python中,可以通过`os.fork()`创建子进程,...

  • 2018-04-02

    python高级 多线程通讯 队列 Queue---来完成多进程间的数据传递 管道 Pipe---方式单...

  • 进程间通信方式

    管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字...

网友评论

      本文标题:使用os pipe管道使python fork多进程之间通信

      本文链接:https://www.haomeiwen.com/subject/gulanktx.html