Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:14:13+00:00 2026-06-18T15:14:13+00:00

I have a NDB model that exposes a few instance methods to manipulate its

  • 0

I have a NDB model that exposes a few instance methods to manipulate its state. In some request handlers, I need to call a few of these instance methods. In order to prevent calling put() more than once on the same entity, the pattern I’ve used so far is similar to this:

class Foo(ndb.Model):
    prop_a = ndb.StringProperty()
    prop_b = ndb.StringProperty()
    prop_c = ndb.StringProperty()

    def some_method_1(self):
        self.prop_a = "The result of some computation"
        return True

    def some_method_2(self):
        if some_condition:
            self.prop_b = "Some new value"
            return True
        return False

    def some_method_3(self):
        if some_condition:
            self.prop_b = "Some new value"
            return True
        if some_other_condition:
            self.prop_b = "Some new value"
            self.prop_c = "Some new value"
            return True
        return False

def manipulate_foo(f):
    updated = False
    updated = f.some_method_1() or updated
    updated = f.some_method_2() or updated
    updated = f.some_method_3() or updated
    if updated:
        f.put()

Basically, each method that can potentially update the entity returns a bool to indicate if the entity has been updated and therefore needs to be saved. When calling these methods in sequence, I make sure to call put() if any of the methods returned True.

However, this pattern can be complex to implement in situations where other subroutines are involved. In that case, I need to make the updated boolean value returned from subroutines bubble up to the top-level methods.

I am now in the process of optimizing a lot of my request handlers, trying to limit as much as possibles the waterfalls reported by AppStat, using as much async APIs as I can and converting a lot of methods to tasklets.

This effort lead me to read the NDB Async documentation, which mentions that NDB implements an autobatcher which combines multiple requests in a single RPC call to the datastore. I understand that this applies to requests involving different keys, but does it also apply to redundant calls to the same entity?

In other words, my question is: could the above code pattern be replaced by this one?

class FooAsync(ndb.Model):
    prop_a = ndb.StringProperty()
    prop_b = ndb.StringProperty()
    prop_c = ndb.StringProperty()

    @ndb.tasklet
    def some_method_1(self):
        self.prop_a = "The result of some computation"
        yield self.put_async()

    @ndb.tasklet
    def some_method_2(self):
        if some_condition:
            self.prop_b = "Some new value"
            yield self.put_async()

    @ndb.tasklet
    def some_method_3(self):
        if some_condition:
            self.prop_b = "Some new value"
            yield self.put_async()
        elif some_other_condition:
            self.prop_b = "Some new value"
            self.prop_c = "Some new value"
            yield self.put_async()

@ndb.tasklet
def manipulate_foo(f):
    yield f.some_method_1()
    yield f.some_method_2()
    yield f.some_method_3()

Would all calls to put_async() be combined into a single put call on the entity? If yes, are there any caveats to using this approach vs sticking to manually checking for an updated return value and calling put once at the end of the call sequence?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:14:14+00:00Added an answer on June 18, 2026 at 3:14 pm

    Well, I bit the bullet and tested these 3 scenarios in a test GAE application with AppStat enabled to look at what RPC calls were being made:

    class Foo(ndb.Model):
        prop_a = ndb.DateTimeProperty()
        prop_b = ndb.StringProperty()
        prop_c = ndb.IntegerProperty()
    
    class ThreePutsHandler(webapp2.RequestHandler):
        def post(self):
            foo = Foo.get_or_insert('singleton')
            foo.prop_a = datetime.utcnow()
            foo.put()
            foo.prop_b = str(foo.prop_a)
            foo.put()
            foo.prop_c = foo.prop_a.microsecond
            foo.put()
    
    class ThreePutsAsyncHandler(webapp2.RequestHandler):
        @ndb.toplevel
        def post(self):
            foo = Foo.get_or_insert('singleton')
            foo.prop_a = datetime.utcnow()
            foo.put_async()
            foo.prop_b = str(foo.prop_a)
            foo.put_async()
            foo.prop_c = foo.prop_a.microsecond
            foo.put_async()
    
    class ThreePutsTaskletHandler(webapp2.RequestHandler):
        @ndb.tasklet
        def update_a(self, foo):
            foo.prop_a = datetime.utcnow()
            yield foo.put_async()
    
        @ndb.tasklet
        def update_b(self, foo):
            foo.prop_b = str(foo.prop_a)
            yield foo.put_async()
    
        @ndb.tasklet
        def update_c(self, foo):
            foo.prop_c = foo.prop_a.microsecond
            yield foo.put_async()
    
        @ndb.toplevel
        def post(self):
            foo = Foo.get_or_insert('singleton')
            self.update_a(foo)
            self.update_b(foo)
            self.update_c(foo)
    
    app = webapp2.WSGIApplication([
        ('/ndb-batching/3-puts', ThreePutsHandler),
        ('/ndb-batching/3-puts-async', ThreePutsAsyncHandler),
        ('/ndb-batching/3-puts-tasklet', ThreePutsTaskletHandler),
    ], debug=True)
    

    The first one, ThreePutsHandler, obviously ends up calling Put 3 times.

    ThreePutsHandler AppStat trace

    However, the 2 other tests that are calling put_async() end up with a single call to Put:

    ThreePutsAsyncHandler AppStat trace
    ThreePutsTaskletHandler AppStat trace

    So the answer to my question is: yes, redundant ndb.Model.put_async() calls are being batched by NDB’s autobatching feature and end up as a single datastore_v3.Put call. And it does not matter if those put_async() calls are made within a tasklet or not.

    A note about the number of datastore write ops being observed in the test results: as Shay pointed out in the comments, there are 4 writes per modified indexed property value plus 1 write for the entity. So in the first test (3 sequential put), we observe (4+1) * 3 = 15 write ops. In the 2 other tests (async), we observe (4*3) + 1 = 13 write ops.

    So the bottom line is that having NDB batch multiple put_async calls for the same entity saves us a lot of latency by having a single call to the datastore, and saves us a few write ops by writing the entity only once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ndb to write a profiling model that logs some data per
I have a model that looks like this: class Foo(ndb.Model): bar = ndb.TextProperty(required=True) #
It seems that db.Key and ndb.Key instances are not the same. I have a
For example: I have an Article model with a repeated title property that stores
I want to get some data from the Google Appengine ndb. I have the
Let's assume that we the following ndb model: class MyModel(ndb.Model): x = ndb.StringProperty() y
I have an AppEngine application that I am considering upgrading to use the NDB
I have an object which inherits from ndb.Model (a Google App Engine thing). This
I have a simple NDB model: from google.appengine.ext import ndb import logging from libs
I have a non-ndb entity that is created and a webapp2 User who owns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.