Transactions test

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import logging
from google.appengine.ext import ndb, deferred
from webapp2 import RequestHandler, WSGIApplication
ENTITY_ID = 1
class MyModel(ndb.Model):
prop = ndb.IntegerProperty(default=0)
def txn(key):
entity = key.get()
entity.prop += 1
entity.put()
def defer_func():
key = ndb.Key(MyModel, ENTITY_ID)
ndb.transaction(lambda: txn(key), retries=0)
class TestHandler(RequestHandler):
def get(self):
MyModel(id=ENTITY_ID).put()
for i in xrange(50000):
deferred.defer(defer_func)
app = WSGIApplication([
('/test', TestHandler)
])