1 """A multithreaded version of os.popen2(). Note that the argument list
2 isn't quite the same."""
3
4
5 import subprocess as S
6
7
8
9
11 __doc__ = """Pseudo-file descriptor. Just like a FD, except
12 that it waits for the process at the end of the pipe
13 to terminate when you close it.
14 """
15
30
31
34
35
38
39
42
43
46
47
48
51
52
55
56
57
58
61
62
64 if not self.closed:
65 self.fd.close()
66 self.closed = True
67 self.p.wait()
68 return self.p.returncode
69
70
73
74
75 -def popen2(path, args, bufsize=0):
76 """Forks off a process, and returns the processes
77 input and output file descriptors. The latter is really
78 a pfd (defined above).
79
80 Path and args are passed directly into os.execvp().
81 """
82
83 p = S.Popen(args, executable=path,
84 stdin=S.PIPE, stdout=S.PIPE, close_fds=True)
85 return (p.stdin, pfd(p))
86
87
89 si, so = popen2("sed", ["sed", "-e", "s/or/er/"])
90 si.write("hello, world!\nsecond line\n")
91 si.close()
92 assert so.readline() == "hello, werld!\n"
93 a = so.readlines()
94 assert len(a) == 1
95 assert a[0] == "second line\n"
96 tmp = so.close()
97 assert tmp == 0, "tmp=%s" % tmp
98
99 if __name__ == '__main__':
100 test()
101 test()
102