Reputation: 2476
I have been facing this problem in app engine. As I am new to App Engine, I don't know whether I am doing it right way or not.
Whenever a new record is inserted into datastore, this newly inserted record doesn't reflect in the application. It will appear only after next reload.
This is the simple code: Adding a user and just printing all users from the User table:
def add_user(name):
u = User(name = name)
u.put()
all_users = [u.name for u in User.all().order('name')]
#On printing 'all_users', it doesn't show recently added user
Is this the common behaviour in App Engine. Does it take time to add new record in datastore? Is there anything wrong in the way record is inserted? Please help..
Upvotes: 1
Views: 166
Reputation: 2279
Operations on the datastore take place in two parts: the commit phase, and the apply phase.
You can read more about it at https://developers.google.com/appengine/docs/python/datastore/overview#Datastore_Writes_and_Data_Visibility
But the short version is that datastore operations return after the commit phase, not after the apply phase. You may have told the datastore to add/update/delete an object, but it'll take some milliseconds for that operation to execute even after the specific op has returned.
You might need to add an artificial delay to your code.
import time
time.sleep(1)
Or You can use @db.transactional
. Using transactional to ensure that DB is consistent before displaying results. https://developers.google.com/appengine/docs/python/datastore/transactions
Upvotes: 1
Reputation: 12986
You should never consider using sleep.
What you are seeing is "eventual consistency" in the HRD. Have a read of "Structuring Data for Strong Consistency" https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency which describes strategies for making strongly consistent queries.
As an aside, if you have just put() a user entity, you don't need to query for it, you already have it.
Upvotes: 2