To use entities in the a transaction, they need to be in the same entity group, and there is a limit to how often you can write an individual entity group (I've heard 1-5 writes per second, though have no real experience with this).In general, you should try to avoid putting things in the same entity group unless you're sure you need to.
If accuracy isn't so important, you could increase the count after writing your post/comment, and just retry it a few times if it fails. Ideally you should handle datastore timeouts anyway (as they're quite common), so retrying a few times will likely solve the problem. You could also write an error to the log (or email an admin) if it still fails after 5 attempts, then manually correct the value later. There's a recipe for auto-retrying datastore calls here: http://appengine-cookbook.appspot.com/recipe/autoretry-datastore-timeouts/ Hope this helps, Danny On Jan 23, 4:14 am, Mark Hildreth <[email protected]> wrote: > That's right, another question re: counting. I thought I had the idea > down until I came across the sharded counter example. Using the > following example: > > http://github.com/DocSavage/sharded_counter/blob/master/counter.py#L131 > > The counter shard has an increment function that contains a > transaction to increment the counter. However, typically a counter is > used to actually COUNT something, meaning elsewhere in the same > request as the count increment you will have had to have made some > change. For example, if you need to count the number of blog entries, > you increment the counter before or after saving a new blog entry. > > Am I correct in assuming that this example would not, then, deal with > the issue of an error happening between the count and the change? As > far as I can tell, the only way to leverage the datastore to make a > truly consistent count is to use an object (say, the "Blog" object) > that keeps the count when you make the change (say, the creation of a > "BlogEntry"). Something like... > > class Blog(db.Model): > count = db.IntegerProperty(default=0) > > class BlogEntry(db.Model): > # All the properties... > > .... > > def tx(): > b = Blog.get_somehow() > be = BlogEntry(parent=b) > b.count += 1 > db.put([b, be]) > > It seems to me that disassociating the thing you're actually counting > from the transaction that increments the counter opens up the > possibility (albeit small) of inconsistent data. Am I missing > something, or are the examples showing the counter being incremented > in their own transaction choosing the latter in the speed vs. accuracy > tradeoff? -- You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
