ndb.put_multi_async 与云 ndb

ndb.put_multi_async with cloud ndb

Google cloud ndb documentation 并没有对异步操作说太多。

以前我会这样

@ndb.toplevel
@flask.route('/', methods=['GET'])
def page():
    for x in xxx:
        ndb.put_multi_async([...])
    return 'Done', 200

顶层装饰器将确保我的异步放置已完成。

如何使用最新的云 ndb 执行此操作?

cloud ndb docs for toplevel

Use of this decorator is largely unnecessary, as you should be using context() which also flushes pending work when exiting the context.

但如果更清楚一点会很有帮助。什么时候还需要使用顶层?

就个人而言,我一直习惯在我的异步 tasklets/operations 上调用 .get_result(),所以这是我从未实际使用过的东西。

我能想到的 toplevel 的唯一用例是,如果您想在到达请求处理程序的末尾之前强制进行刷新(因为在请求处理程序的末尾,您应该退出上下文)。在下面的示例中,我们希望 operation_1 中的放置在 operation_2 开始之前完成:

@ndb.toplevel
def operation_1():
    for x in xxx:
        ndb.put_multi_async([...])

@flask.route('/', methods=['GET'])
def page():
    operation_1()
    operation_2()
    return 'Done', 200

这可能对 Google 云任务的请求处理程序很有用,它可以 运行 长达 10 分钟,因此您可以在那里做很多事情。

documentation 中所述 NDB 异步操作:

As a convenience, you can decorate the request handler with @ndb.toplevel. This tells the handler not to exit until its asynchronous requests have finished. ... Using a toplevel application is more convenient than all its handler functions.

这在将 NDB 客户端库 与 Python 2 一起使用时很方便,正如您所说:

the toplevel decorator would make sure that my async puts were done

现在使用 Cloud NDB library, as shown in this answer,

each NDB operation needs to be wrapped in a context manager:

 with ndb_client.context(): # <- you need this line
    cls.get_or_insert('master') 

这就是为什么documentation说使用顶层装饰器

is largely unnecessary, as you should be using context()

因为上下文装饰器替换了它,它将

flush pending work_ as async operations.

Client NDB documentation 中所述:

The context is used to manage the connection to Google Cloud Datastore, an event loop for asynchronous API calls, runtime caching policy, and other essential runtime state.

最后,如ndb Migration notes所述:

The biggest difference is in establishing a runtime context for your NDB application. The Google App Engine Python 2.7 runtime had a strong assumption that all code executed inside a web framework request-response cycle, in a single thread per request. In order to decouple from that assumption, Cloud NDB implements explicit clients and contexts.