Created
October 13, 2016 03:38
-
-
Save tag1216/6ca4861e8894d41425ba21981f106f44 to your computer and use it in GitHub Desktop.
Python subprocessのstdoutとstderrの両方をリアルタイムで取得
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import time | |
print("start") | |
i = 1 | |
while True: | |
if i % 5 == 0: | |
print(i, flush=True, file=sys.stderr) | |
else: | |
print(i, flush=True) | |
i += 1 | |
time.sleep(1.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import subprocess | |
import sys | |
from subprocess import PIPE | |
from threading import Thread | |
def read_stream(in_file, out_file): | |
for line in in_file: | |
print(line.strip(), file=out_file, flush=True) | |
p = subprocess.Popen("./test_stream.py", stdin=sys.stdin, stdout=PIPE, stderr=PIPE, universal_newlines=True) | |
Thread(target=read_stream, args=(p.stdout, sys.stdout)).start() | |
Thread(target=read_stream, args=(p.stderr, sys.stderr)).start() | |
# p = subprocess.Popen("./test_stream.py", stdin=sys.stdin, stdout=PIPE, stderr=subprocess.STDOUT, universal_newlines=True) | |
# Thread(target=read_stream, args=(p.stdout, sys.stdout)).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment