-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_std.py
52 lines (44 loc) · 1.3 KB
/
async_std.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
'''
Read from a PIPE async.
See https://stackoverflow.com/questions/375427/a-non-blocking-read-on-a-subprocess-pipe-in-python
'''
import subprocess as sp
from threading import Thread
from io import BytesIO
class Reader(Thread):
def __init__(self, pipe: BytesIO) -> None:
super().__init__()
self.pipe = pipe
self._buffer = BytesIO()
def run(self):
try:
for line in iter(self.pipe.readline, b''):
self._buffer.write(line)
except BrokenPipeError:
return
def get(self):
self.join()
self._buffer.seek(0)
return self._buffer
class PopenAsyncStd(sp.Popen):
def __init__(self, *args, **kw):
super().__init__(
*args,
stdout=sp.PIPE, stderr=sp.PIPE,
**kw,
)
self.outReader = Reader(self.stdout)
self.errReader = Reader(self.stderr)
self.outReader.start()
self.errReader.start()
def reportCollectedOutErr(self):
print(self, 'out:')
print(self.outReader.get().read().decode())
print(self, 'err:')
print(self.errReader.get().read().decode())
def test():
with PopenAsyncStd(['python']) as p:
p.wait()
p.reportCollectedOutErr()
if __name__ == '__main__':
test()