RPCRequests and datastore
I'm getting started with Google App Engine. I want to make an AJAX chat.
class ChatMessage(db.Model):
message = db.StringProperty()
created = db.DateTimeProperty(auto_now_add=True)
class RPCHandler(webapp.RequestHandler):
##get message every 4 seconds
def get(self):
que = db.Query(ChatMessage).order('-created')
chat_list = que.fetch(limit=3)
jencoded_chat_list = gaejsonEncoder.encode(chat_list) ##my own module
self.response.out.write(jencoded_chat_list)
RESULT: message3,message2,message1
This is what I intended.
I wanted to add POST ajax Request, so added RPCHandler2.
class RPCHandler2(webapp.RequestHandler):
def post(self):
msg = self.request.get('message')
if msg == '':
return
newchat = ChatMessage(message=msg)
newchat.put()
que = db.Query(ChatMessage).order('-created')
chat_list =开发者_Python百科 que.fetch(limit=3))
jencoded_chat_list = gaejsonEncoder.encode(chat_list)
self.response.out.write(jencoded_chat_list)
I write "POST Message!" in textarea and click the button,
RESULT: POST Message!,message3,message2
This is what I wanted.
But 4 seconds later, 'GET' started and
RESULT: message3,message2,message1
Why 'GET' couldn't get the new message from datastore?
Thanks in advance.
If you are using the high replication datastore you might be facing an "eventual consistency" problem. The datastore replica handling the get request may not have received the recently submitted data yet.
If you put the newchat.put()
inside of a transaction it's more likely that future gets will be consistent.
精彩评论