Python Flask SocketIO broadcasting outside of @socketio context

被刻印的时光 ゝ 提交于 2019-12-24 02:13:04

问题


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)

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.

EDIT: It seems to be a known issue https://github.com/miguelgrinberg/Flask-SocketIO/pull/213


回答1:


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')


来源:https://stackoverflow.com/questions/35502133/python-flask-socketio-broadcasting-outside-of-socketio-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!