下面是一个将mesg发送到浏览器的简单应用程序.如果有来自redis频道的新mesg,则将以非阻塞方式发送其他明智的发送最后知道值.
但我做错了什么.有人可以帮我理解
from gevent import monkey, Greenlet
monkey.patch_all()
from flask import Flask,render_template,request,redirect,url_for,abort,session,Response,jsonify
app = Flask(__name__)
myglobaldict = {'somedata':''}
class RedisLiveData:
def __init__(self, channel_name):
self.channel_name = channel_name
self.redis_conn = redis.Redis(host='localhost', port=6379, db=0)
pubsub = self.redis_conn.pubsub()
gevent.spawn(self.sub, pubsub)
def sub(self,pubsub):
pubsub.subscribe(self.channel_name)
for message in pubsub.listen():
gevent.spawn(process_rcvd_mesg, message['data'])
def process_rcvd_mesg(mesg):
print "Received new message %s " % mesg
myglobaldict['somedata'] = mesg
g = RedisLiveData("test_channel")
@app.route('/latestmessage')
def latestmessage():
return Response(myglobaldict,mimetype="application/json")
if __name__ == '__main__':
app.run()
在javascript方面我只是使用一个简单的$.ajax来查看消息.
但是,即使在redis更新之后,客户端http:// localhost:5000 / latestmessage也会收到旧消息.
最佳答案 它应该是缓存问题.
您可以将时间戳或随机数添加到从ajax发送的请求http:// localhost:5000 / latestmessage?t = timestamp.