OS File 中的 os.pipe()函数

首页 / Python2入门教程 / OS File 中的 os.pipe()函数

Python方法Pipe()创建一个管道并返回一对分别可用于读取和写入的文件描述符(r,w

os.pipe() - 语法

os.pipe()

os.pipe() - 返回值

此方法返回一对文件描述符。

链接:https://www.learnfk.comhttps://www.learnfk.com/python/os-pipe.html

来源:LearnFk无涯教程网

os.pipe() - 示例

以下示例显示了Pipe()方法的用法。

#!/usr/bin/python

import os, sys

print "The child will write text to a pipe and "
print "the parent will read the text written by child..."

# 文件描述符 r, w 用于读写
r, w=os.pipe() 

processid=os.fork()
if processid:
   # 这是父进程关闭文件描述符 w
   os.close(w)
   r=os.fdopen(r)
   print "Parent reading"
   str=r.read()
   print "text =", str   
   sys.exit(0)
else:
   # 这是子进程
   os.close(r)
   w=os.fdopen(w, 'w')
   print "Child writing"
   w.write("Text written by child...")
   w.close()
   print "Child closing"
   sys.exit(0)

当无涯教程运行上面的程序时,它产生以下输出-

The child will write text to a pipe and
the parent will read the text written by child...
Parent reading
Child writing
Child closing
text=Text written by child...

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学微服务 -〔胡忠想〕

SQL必知必会 -〔陈旸〕

研发效率破局之道 -〔葛俊〕

软件设计之美 -〔郑晔〕

跟月影学可视化 -〔月影〕

成为AI产品经理 -〔刘海丰〕

全链路压测实战30讲 -〔高楼〕

自动化测试高手课 -〔柳胜〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)