Created
February 17, 2020 09:33
-
-
Save tony612/706f004461580b7059264002f4024445 to your computer and use it in GitHub Desktop.
Python read from stdin
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
def read_until_eof(): | |
buffer = '' | |
batch_size = 1024 | |
while True: | |
ready, _, _ = select.select([sys.stdin], [], [], 0.0) | |
if sys.stdin not in ready: | |
continue | |
# Don't know why, but sys.stdin.read() and sys.stdin.readline() | |
# will cause EPIPE when writting to stdout | |
new_data = os.read(sys.stdin.fileno(), batch_size) | |
data_len = len(new_data) | |
if data_len == 0: | |
if len(buffer) == 0: | |
continue | |
else: | |
yield buffer | |
buffer = '' | |
buffer += new_data.decode() | |
if data_len < batch_size: | |
yield buffer | |
buffer = '' |
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
def readline(): | |
buffer = '' | |
while True: | |
ready, _, _ = select.select([sys.stdin], [], [], 0.0) | |
if sys.stdin not in ready: | |
continue | |
# Don't why, but sys.stdin.read() and sys.stdin.readline() | |
# cause EPIPE when writting to stdout | |
new_data = os.read(sys.stdin.fileno(), 1024) | |
if len(new_data) == 0: | |
if len(buffer) == 0: | |
break | |
continue | |
buffer += new_data.decode() | |
result = buffer.split('\n', 1) | |
if len(result) == 1: | |
continue | |
line = result[0] | |
buffer = result[1] | |
yield line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment