os.pipe() 函数在 Python 中起什么作用?
pythonserver side programmingprogramming
os.pipe() 方法创建一个管道并返回一对文件描述符 (r, w),分别用于读取和写入。
示例
import os, sys print "子进程将向管道写入文本," print "父进程将读取子进程写入的文本..." # 用于读取和写入的文件描述符 r, w r, w = os.pipe() processid = os.fork() # 这是父进程 if processid: os.close(w) r = os.fdopen(r) print "Parent reading" str = r.read() print "text =", str sys.exit(0) else: # This is the child process 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)
输出
您将获得输出:
Parent reading Child writing Child closing text = Text written by child...