-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_pipe.py
55 lines (45 loc) · 1.08 KB
/
os_pipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-17 14:56:59
# @copyright by hoojo@2018
# @changelog Added python3 `os file -> pipe` example
'''
概述
os.pipe() 方法用于创建一个管道, 返回一对文件描述符(r, w) 分别为读和写。
语法
pipe()方法语法格式如下:
os.pipe()
参数
无
返回值
返回文件描述符对。
'''
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()
# 获取线程id
processid = os.fork()
print('进程类型:%s' % processid)
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)