python-无名管道进程通信

 1 #!/usr/bin/python
 2 #coding=utf-8
 3 import sys,os
 4 from time import sleep
 5 
 6 (r,w)=os.pipe()   #创建无名管道,返回两个整数,代表两个管道文件,且代表的功能是(r,w)
 7 pid=os.fork()
 8 
 9 if pid<0:
10     print "fail to fork"
11 elif pid==0:
12     print "child",os.getpid()
13     os.close(w)   #关闭文件描述符
14     r=os.fdopen(r,"r") #把底层的文件描述符转换为文件对象。
15     while True:
16         buf=r.readline()
17         print "buf:",buf
18         sys.stdout.flush()
19     print "child close"
20 else:
21     print "parent:",os.getpid()
22     os.close(r)
23     w=os.fdopen(w,'w')
24     while True:
25         buf=sys.stdin.readline()
26         w.write(buf)
27         w.flush()
28 #无名管道是不会创建实体文件
原文地址:https://www.cnblogs.com/chengyunshen/p/7195918.html