Search code examples
pythongoogle-app-enginedatastore

put_async slow in Google App Engine


I recently replaced many sycnronous db.put calls in an appengine app w/ the async calls. I don't need to use this data for the rest of the request, so initially I was just calling the put_async function in a fire-and-forget manner. Then I came across this post on SO: Appengine: put_async doesn't work (at least in the development server)? , and the response from an app engine engineer saying you have to call wait or get_result on the async object to guarantee it's written.

So in my testing all the put_async calls were working fine, but I wanted to be sure so I added a global module with a variable that I access across all other modules and store the async ref in every time a call was made to put_async in the app. ie: APP_GLOBALS.async_db_calls.append( db.put_async( whatever_db_model ) )

Then I registered a shutdown function with atexit that iterates over all the asycn calls for this request and calls wait() on them to guarantee they were written to the datastore.

This works but I noticed an extreme slowdown in performance after this change... Does anybody have any insights on this, or know a better way to go about using the asycncalls for write without having the shutdown function?


Solution

  • The NDB API has features that will greatly assist you with this. However, you will always have to wait for the RPC calls to finish before the request is finished, this is not something you can avoid. Calling the *_async counterparts of the datastore API does not let you do work outside of requests.

    http://code.google.com/appengine/docs/python/ndb/async.html