Simple Flask Stream with Requests Consumer

pip install flask

server.py


from flask import Flask

import subprocess

from flask import stream_with_context, request, Response


app = Flask(__name__)

p = subprocess.Popen(['salt-run','state.event', 'pretty=True'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

@app.route("/reactor/stream", methods=["GET"])
def master_reactor():

    def generate():
      retcode = p.poll() #returns None while subprocess is running
      line = p.stdout.readline()
      yield line
      if(retcode is not None):
          return

    return Response(stream_with_context(generate()))

if __name__ == "__main__":
    app.debug = True
    app.run(host="0.0.0.0")


client.py

import requests
from contextlib import closing

while True:
    with closing(requests.get('http://localhost:5000/reactor/stream', stream=True)) as r:
        print r.text