问题描述
我正在尝试在外部值更改时发送广播.Camonitor 在值改变时调用回调,我想通知所有连接的客户端该值已经改变,他们需要刷新.
I'm trying to send a broadcast when an outside value changes. Camonitor calls the callback when the value changes, and I want to notify all connected clients that the value has changed and they need to refresh.
from flask import Flask
from epics import caget, caput, camonitor
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)
@socketio.on('connect')
def local_client_connect():
print "Client connected"
def update_image_data(pvname, value, **kw):
# broadcast event
print "Sending broadcast"
socketio.emit('newimage')
if __name__ == "__main__":
# start listening for record changes
camonitor("13SIM1:cam1:NumImagesCounter_RBV", writer=None, callback=update_image_data)
socketio.run(app, debug=True)
我的回调函数在值改变时成功调用,但广播不起作用.如果我将 socketio.emit 移动到 local_client_connect,它就可以工作.
My callback function is successfully called when the value changes, but the broadcast doesn't work. If I move the socketio.emit to local_client_connect, it works.
这似乎是一个已知问题 https://github.com/miguelgrinberg/Flask-SocketIO/pull/213
It seems to be a known issue https://github.com/miguelgrinberg/Flask-SocketIO/pull/213
推荐答案
是的,这是一个已知问题,但它有一个非常简单的解决方法:
Yes, this is a known issue, but it has a very simple workaround:
def update_image_data(pvname, value, **kw):
# broadcast event
print "Sending broadcast"
with app.app_context():
socketio.emit('newimage')