Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 使用Google应用程序引擎进行事务处理_Google App Engine_Transactions_Google Cloud Datastore_App Engine Ndb - Fatal编程技术网

Google app engine 使用Google应用程序引擎进行事务处理

Google app engine 使用Google应用程序引擎进行事务处理,google-app-engine,transactions,google-cloud-datastore,app-engine-ndb,Google App Engine,Transactions,Google Cloud Datastore,App Engine Ndb,我有两个任务(a,b)应该在事务中执行,以避免不一致。我可能需要执行其中一项任务或同时执行两项任务 在下面的代码中,函数a执行任务a和函数b执行任务b @ndb.transactional def function_a(): # basic validations # read entity of type A # make changes to entity # write entity return @ndb.transactional def fu

我有两个任务(
a
b
)应该在事务中执行,以避免不一致。我可能需要执行其中一项任务或同时执行两项任务

在下面的代码
中,函数a
执行
任务a
函数b
执行
任务b

@ndb.transactional
def function_a():
    # basic validations
    # read entity of type A
    # make changes to entity
    # write entity
    return

@ndb.transactional
def function_b():
    # basic validations
    # read entity of type B
    # make changes to entity
    # write entity
    return
所以如果我需要一起执行任务a和b。我可以执行以下操作:-

def function_ab():
    function_a()
    function_b()
    return
但是这个实现不会同时保持
A
B
的一致性,所以我也需要使它具有事务性

@ndb.transactional
def function_ab():
    function_a()
    function_b()
    return
因此,在这样的实现中,为了代码的可重用性,多次使用事务(一次使用
function\u ab
,一次使用单个
function\u a
function\u b
)。如果我不追求代码的可重用性,我可以采取如下措施:-

@ndb.transactional
def function_ab():
    # basic validations
    # read entity of type A
    # make changes to entity
    # write entity

    # basic validations
    # read entity of type B
    # make changes to entity
    # write entity    
    return

我不太了解AppEngine中的事务是如何工作的,所以我的问题是,使用具有代码可重用性的事务是否有任何缺点(性能等),而我不追求代码可重用性,代码可重用性方法真的有效吗?

您不能嵌套事务,您的事务
函数\u ab()
将给出一个错误

但是您仍然可以通过确保只有顶级函数是事务性的,而内部函数只是检查它们是否确实在事务中,来安排代码以供重用。也许是这样的:

def function_a():
    assert ndb.in_transaction()
    return

def function_b():
    assert ndb.in_transaction()
    return

@ndb.transactional
def function_ab(do_a, do_b):
    if do_a:
        function_a()
    if do_b:
        function_b()

您的使用模式实际上非常适合使用
ndb
async tasklet,甚至更高效。查找中的
get\u cart\u plus\u offers()
示例。

app engine中的嵌套事务工作正常,我通过编写一些代码进行了测试,get\u cart\u plus\u offers()用于从数据库读取数据,而不是将数据写入数据库。可能(我不确定)嵌套事务在您的案例中起作用,因为这两个函数不会导致冲突,因为它们在不同的实体上运行,不确定。但这不是我的情况。只要将
get\u async()。如果tasklet执行读或写操作,这并不重要,关键是并行执行它们(这是可能的,因为它们在不同的实体上操作)。否则它们将被序列化。在高操作量的情况下,可能会对性能产生巨大影响。因此,如果我们对不同的实体使用嵌套事务,并且与答案中建议的方式相比没有任何错误,我们只使用一个事务,是否存在任何性能影响?我所指的性能影响与嵌套事务无关(即使在同一事务中也会发生)-它来自于使用序列化的同步DB访问,而不是可以并行发生的异步Tasklet。