python-无名管道进程通信

时间:2023-03-09 23:37:53
python-无名管道进程通信
 #!/usr/bin/python
#coding=utf-8
import sys,os
from time import sleep (r,w)=os.pipe() #创建无名管道,返回两个整数,代表两个管道文件,且代表的功能是(r,w)
pid=os.fork() if pid<0:
print "fail to fork"
elif pid==0:
print "child",os.getpid()
os.close(w) #关闭文件描述符
r=os.fdopen(r,"r") #把底层的文件描述符转换为文件对象。
while True:
buf=r.readline()
print "buf:",buf
sys.stdout.flush()
print "child close"
else:
print "parent:",os.getpid()
os.close(r)
w=os.fdopen(w,'w')
while True:
buf=sys.stdin.readline()
w.write(buf)
w.flush()
#无名管道是不会创建实体文件