pip install Flask Flask-Sockets gunicorn pyepics
gunicorn -k flask_sockets.worker index:app
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
var socket = new WebSocket('ws://127.0.0.1:8000/monitor') | |
socket.onmessage = function(event) { | |
var data = JSON.parse(event.data) | |
document.getElementById('beam-current').innerHTML = data['value'] | |
} | |
}) | |
</script> | |
</head> | |
<body> | |
<p>Beam Current: <span id='beam-current'></span></p> | |
</body> | |
</html> |
#-*- coding: utf-8 -*- | |
from flask import Flask, render_template | |
from flask_sockets import Sockets | |
from geventwebsocket.exceptions import WebSocketError | |
from epics import PV | |
import json | |
app = Flask(__name__) | |
sockets = Sockets(app) | |
ws_conns = [] | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
@sockets.route('/monitor') | |
def echo_socket(ws): | |
ws_conns.append(ws) | |
while True: | |
message = ws.receive() | |
if message is None: | |
break | |
ws_conns.remove(ws) | |
ws.close() | |
def callback(pvname, value, **kws): | |
message = json.dumps({'pvname': pvname, 'value': value}) | |
for ws in ws_conns: | |
try: | |
ws.send(message) | |
except WebSocketError: | |
pass | |
pv = PV('SR11BCM01:CURRENT_MONITOR', callback=callback) |
Any idea on if this would work with multiple PVs? Been trying it for shutter PV's and not having any luck. Can read them fine using pyepics but unable to get it to work in the callback.