Skip to content

Instantly share code, notes, and snippets.

@pfig
Last active April 19, 2017 17:09
Show Gist options
  • Save pfig/7c035f60feeed82d5edd00a0bc35dc4c to your computer and use it in GitHub Desktop.
Save pfig/7c035f60feeed82d5edd00a0bc35dc4c to your computer and use it in GitHub Desktop.
HTTP server producing a stream of RecordIO data
#
# Run with gunicorn -k eventlet server:app
#
# Dependencies:
# eventlet==0.21.0
# Flask==0.12.1
# gunicorn==19.7.1
#
import time
import random
from flask import Flask, Response
import eventlet
eventlet.monkey_patch()
subjects = ['Alice', 'Bob', 'Eve']
verbs = ['eats', 'steals', 'paints']
objects = ['an apple', 'cookies', 'the elephant']
app = Flask('chunky_bacon')
@app.route('/stream')
def record_io_stream():
def generate():
total = 0
while (total <= 200):
output = '{} {} {}'.format(
random.choice(subjects),
random.choice(verbs),
random.choice(objects))
much = len(output)
output = '{}\n{}'.format(much, output)
total += much
yield output
time.sleep(1)
return Response(generate(), mimetype='text/plain')
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment