I have a Flask app that sends an event stream of temperature data to the client. Since the application controls a kiln, it's important that the user can access the job specific controls (Stop, Pause, Resume, etc).
Right now, the chart updates with information coming from the event source, but when I try to change routes to control the kiln itself, the request is stuck in "Pending".
Any advice?
Relevant code:
commands.py
def generate_data(self):
...
while True:
time.sleep(1)
temp_data = json.loads(data)
temp = temp_data['temp']
json_data = json.dumps({'time': datetime.now(), 'temp': temp})
yield f"data:{json_data}\n\n"
views.py
...
@app.route('/chart_data')
def chart_data():
kc = kiln_command.KilnCommand()
return Response(kc.generate_data(), mimetype='text/event-stream')
show_jobs.html
...
<script>
...
const context = document.getElementById('canvas').getContext('2d')
const lineChart = new Chart(context, config)
const source = new EventSource("/chart_data")
source.onmessage = function (event) {
const data = JSON.parse(event.data);
...
</script>