Python Flask SocketIO 在@socketio 上下文之外广播 [英] Python Flask SocketIO broadcasting outside of @socketio context

查看:48
本文介绍了Python Flask SocketIO 在@socketio 上下文之外广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在外部值更改时发送广播.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')

这篇关于Python Flask SocketIO 在@socketio 上下文之外广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆