OS File 中的 os.pipe()函数

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

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

os.pipe() - 语法

os.pipe()

os.pipe() - 返回值

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

os.pipe() - 示例

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

无涯教程网

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

来源:LearnFk无涯教程网

#!/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...

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

技术教程推荐

白话法律42讲 -〔周甲徳〕

编辑训练营 -〔总编室〕

Netty源码剖析与实战 -〔傅健〕

深度学习推荐系统实战 -〔王喆〕

Django快速开发实战 -〔吕召刚〕

物联网开发实战 -〔郭朝斌〕

现代C++20实战高手课 -〔卢誉声〕

深入拆解消息队列47讲 -〔许文强〕

结构会议力 -〔李忠秋〕

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