赞
踩
我有一个Python Flask应用程序,当特定事件被触发时,它应该将服务器端事件(SSE)推送到我的网页(例如,通过间隔计时器,当用户按下页面上的特定按钮等)。Flask中的服务器端事件(SSE)不断请求而不是请求
但是,我不会让Flask只在我请求应用程序时才推送消息。相反,在启动应用程序后,它每隔几秒就会不断向服务器推送消息,这不是我想要的。
我这种行为的小例子,看起来是这样的:
from flask import Flask
from flask import render_template, Response
from random import randint
app = Flask(__name__)
# function to push data to the server
def pushData():
# randint is just to make every message not look the same
number = randint(0,9)
print('I push data to the server: {0}'.format(number))
yield 'data: %s\n\n' % 'I am data that has been pushed to the server: {0}'.format(number)
# provide SSE stream to the web browser
@app.route('/listenForPushes')
def stream():
return Response(pushData(), mim
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。