通过命令管道,在进程之间进行通信的简单例子

简介: write_to_named_pipe.py import os if __name__ == "__main__": named_pipe = "my_pipe" if not os.

write_to_named_pipe.py

import os


if __name__ == "__main__":
    named_pipe = "my_pipe"

    if not os.path.exists(named_pipe):
        os.mkfifo(named_pipe)

    def write_message(input_pipe, message):
        fd = os.open(input_pipe, os.O_WRONLY)
        os.write(fd, (message +  str(os.getpid())))
        os.close(fd)
    write_message(named_pipe, "from write_pipe...")

  

read_from_named_pipe.py

import os

if __name__ == "__main__":
    named_pipe = "my_pipe"
    def read_message(input_pipe):
        fd = os.open(input_pipe, os.O_RDONLY)
        message = ("I pid [%d] received a message => %s" %(os.getpid(), os.read(fd, 22)))
        os.close(fd)
        return message
    print read_message(named_pipe)

  

 

目录
相关文章
|
1月前
|
Shell Linux 调度
【Shell 命令集合 系统管理 】Linux 调整进程优先级 renice命令 使用指南
【Shell 命令集合 系统管理 】Linux 调整进程优先级 renice命令 使用指南
41 0
|
1月前
|
存储 监控 Linux
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 查看当前正在运行的进程信息 ps命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 查看当前正在运行的进程信息 ps命令 使用指南
42 0
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 向进程发送信号 kill命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 向进程发送信号 kill命令 使用指南
31 0
|
3天前
|
Linux
【linux进程间通信(一)】匿名管道和命名管道
【linux进程间通信(一)】匿名管道和命名管道
|
24天前
|
Web App开发 人工智能 Ubuntu
【Linux】Linux启动/查看/结束进程命令(详细讲解)
【Linux】Linux启动/查看/结束进程命令(详细讲解)
|
1月前
|
Unix Shell Linux
【Shell 命令集合 系统管理 】⭐⭐Linux 让进程休眠 sleep命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐Linux 让进程休眠 sleep命令 使用指南
37 0
|
1月前
|
安全 Shell Linux
【Shell 命令集合 系统管理 】Linux 终止或向进程发送信号 skill命令 使用指南
【Shell 命令集合 系统管理 】Linux 终止或向进程发送信号 skill命令 使用指南
31 0
|
1月前
|
算法 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示进程之间的关系 pstree命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示进程之间的关系 pstree命令 使用指南
29 0
|
1月前
|
监控 Linux Shell
【Shell 命令集合 系统管理 】⭐Linux 显示系统中的进程信息 procinfo命令 使用指南
【Shell 命令集合 系统管理 】⭐Linux 显示系统中的进程信息 procinfo命令 使用指南
26 0
|
1月前
|
Shell Linux 调度
【Shell 命令集合 系统管理 】⭐Linux 调整进程优先级 nice命令 使用指南
【Shell 命令集合 系统管理 】⭐Linux 调整进程优先级 nice命令 使用指南
34 0