Executing code outside of a transaction (python + NDB)

60 views
Skip to first unread message

Ofri Wolfus

unread,
Jan 15, 2014, 6:34:04 AM1/15/14
to google-a...@googlegroups.com
Hi everyone,

I'm new to python, appengine and this group so please bear with me :)

I have a function that updates a set of entities from different entity groups. The set's size will get bigger than the allowed size of cross-group transactions. The issue is, this function may get called from within an existing transaction.

Now the actual task of the function is not important to the running transaction, and so it should be executed outside of it. What's the best way to accomplish such a thing without restructuring the existing code?

My current solution (which is quite bad IMO) is to run the worker function as a tasklet, then wrap it in an outer function that blocks on the tasklet's result. Something like the following (typed in mail):

@ndb.tasklet
def worker_func():
    # do some work
    raise ndb.Return(True)

def wrapper():
    assert worker_func() == True

Thanks,
Ofri

timh

unread,
Jan 15, 2014, 8:03:34 PM1/15/14
to google-a...@googlegroups.com
Why not just use the non transactional decorator, 

@ndb.non_transactional
@ndb.non_transactional(allow_existing=True)
Decorator to ensure that a function runs outside a transaction.

Arguments:

allow_existing
If True (the default) and if the decorated function is called by code in a transaction, the function runs independent of the transaction. If False and if the decorated function is called by code in a transaction, it raises an exception.
Reply all
Reply to author
Forward
0 new messages