Skip to content

Instantly share code, notes, and snippets.

@akiross
Created October 15, 2018 13:53
Show Gist options
  • Save akiross/247a1a50d355323caf7f9d835d32f7a0 to your computer and use it in GitHub Desktop.
Save akiross/247a1a50d355323caf7f9d835d32f7a0 to your computer and use it in GitHub Desktop.
A demo code to show what happens when pipe buffer get filled.
"""This code runs a subprocess that produces a large amount of data, while
the main process reads the data at a slower pace. What happens on the long run?
This program is a demo showing what happens when a pipe buffer gets filled.
"""
import io
import sys
import time
import textwrap
import tempfile
import subprocess
if __name__ == '__main__':
# Program to run, will write data both on stdout and stderr
sub_program = textwrap.dedent('''
import sys
import time
i = 0
while True:
print("I am still writing number", i, file=sys.stderr)
print("Data" , i, "is data", i, 'filler ' * 20)
i += 1
time.sleep(0.01) # Do not flood too much
''')
# Open a temp file with source code
with tempfile.NamedTemporaryFile(mode='wt') as program_file:
# Write the source to the file
print(sub_program, file=program_file)
# Ensure code is there
program_file.flush()
# Start a python interpreter to run that code
proc = subprocess.Popen(['python3', '-u', program_file.name],
stdout=subprocess.PIPE,
stderr=sys.stderr)
# Read one line every second
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"):
print(line.strip())
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment