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
29
30
33
34
37
38
41
42
45
46
47
50
51
54
55
56
57
60
61
63 if not self.closed:
64 self.fd.close()
65 self.closed = True
66 self.p.wait()
67 return self.p.returncode
68
69
72
73
74 -def popen2(path, args, bufsize=0):
75 """Forks off a process, and returns the processes
76 input and output file descriptors. The latter is really
77 a pfd (defined above).
78
79 Path and args are passed directly into os.execvp().
80 """
81
82 p = S.Popen(args, executable=path,
83 stdin=S.PIPE, stdout=S.PIPE, close_fds=True)
84 return (p.stdin, pfd(p))
85
86
88 si, so = popen2("sed", ["sed", "-e", "s/or/er/"])
89 si.write("hello, world!\nsecond line\n")
90 si.close()
91 assert so.readline() == "hello, werld!\n"
92 a = so.readlines()
93 assert len(a) == 1
94 assert a[0] == "second line\n"
95 tmp = so.close()
96 assert tmp is None, "tmp=%s" % tmp
97
98 if __name__ == '__main__':
99 test()
100 test()
101