Skip to content

Instantly share code, notes, and snippets.

@srossross
Created October 19, 2015 15:45
Show Gist options
  • Save srossross/aa1e874bc6a55b4dc6e7 to your computer and use it in GitHub Desktop.
Save srossross/aa1e874bc6a55b4dc6e7 to your computer and use it in GitHub Desktop.
import subprocess as sp
from binstar_build_client.worker.utils.timeout import Timeout
import time
import io
import os
class FakeBuildLog(object):
"""
This class should actually post to anaconda-server
but here it is just to show the output.
"""
def __init__(self):
self.buffer = io.BytesIO()
self.closed = False
def writable(self):
return True
def getvalue(self):
return self.buffer.getvalue()
def write(self, data):
print("write to woof")
self.buffer.write(b'--\n')
n = self.buffer.write(data)
self.buffer.write(b'--\n')
return n
def read_with_timout(p0, output, timeout=4, iotimeout=2, flush_iterval=1):
@Timeout(timeout)
def timer():
print("p0.kill() || timeout")
output.write(b"p0.kill() || timeout")
p0.kill()
@Timeout(iotimeout)
def iotimer():
print("p0.kill() || iotimeout")
output.write(b"p0.kill() || iotimeout")
p0.kill()
with timer, iotimer:
line = p0.stdout.readline()
last_flush = time.time()
while line:
iotimer.tick()
print('line', line)
output.write(line)
if time.time() - last_flush > flush_iterval:
last_flush = time.time()
output.write(b"b_output.flush()")
output.flush()
line = p0.stdout.readline()
output.flush()
p0.wait()
def main():
stream = FakeBuildLog()
# Wrap the build log in a BufferedWriter so url requests don't get sent for every line
# of output
b_output = io.BufferedWriter(stream)
env = os.environ.copy()
env['PYTHONUNBUFFERED'] = '1'
p0 = sp.Popen(['python', 'timedoutput.py'], stdout=sp.PIPE, env=env)
read_with_timout(p0, b_output)
print("-- b_output --")
print(stream.getvalue().decode())
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment