【问题标题】:Using always async ndb operations in ndb gae在 ndb gae 中始终使用异步 ndb 操作
【发布时间】:2014-05-29 10:35:37
【问题描述】:

我正在使用 gae 和 ndb 进行开发,我发现我必须为 ndb 获取和查询编写大量代码。有时我需要两个版本(有时是 3 个或更多)。

假设我在其中有以下模型和函数:

class MainEnt(ndb.Model):
    """Main entity. The parent is the user_key"""
    name = ndb.StringProperty()
    child = ndb.KeyProperty()

    def get_created_by(self, user_id):
        """Get the MainEnt's created by user_id"""
        # stuff... getting the query and returning the results

    def get_created_by_async(self, user_id):
        """Get async the MainEnt's created by user_id"""
        # stuff... getting the query async and returning a future

    @ndb.tasklet
    def get_childs_async_tasklet(self, mainent_key):
        """Get async the childs of a MainEnt"""
        # stuff... yielding the query async. and raise ndb.Return()...

    @ndb.tasklet
    def get_created_by_async_tasklet(self, user_id):
        """Get async the MainEnt's created by user_id"""
        # stuff... yielding the query async. and raise ndb.Return()...

    @ndb.tasklet
    def get_created_by_async_tasklet_with_childs(self, user_id):
        """Get async the MainEnt's created by user_id and its childs"""
        # stuff... yielding the query async. calling get_childs.. and raise ndb.Return()...

    @ndb.tasklet
    def get_voted_by_async_tasklet(self, user_id):
        """Get async the MainEnt's voted by user_id"""
        # stuff... yielding the query async raise ndb.Return()...

    @ndb.tasklet
    def get_voted_by_async_tasklet_with_childs(self, user_id):
        """Get async the MainEnt's voted by user_id and it's childs"""
        # stuff... yielding the query async, calling get_childs.. and raise ndb.Return()...

    @ndb.tasklet
    def get_created_and_voted_by_async_tasklet(self, user_id):
        """yield get_created_by_async_tasklet and get_voted_by_async_tasklet in parallel"""
        # stuff... yielding the other two functions and return results...

class ChildEnt(ndb.Model):
    name = ndb.StringProperty()

如您所见,MainEnt 具有三个返回“相同”(有时更多)结果但用于不同上下文的方法。

1) 仅当我需要获取结果并且获取该结果是唯一的“ndb 操作”时才使用同步功能。

2) async 函数仅在我需要获取结果时使用,但我还执行了一些我想要重叠的其他 ndb 查询。

3) tasklet 异步功能仅在我需要获取结果、获取此结果的子实体以及可能还执行一些其他 ndb 操作时使用。

因为我发现写了太多函数...只写一个异步tasklet函数是正确的,在前面的每种情况下都会被调用??

我的意思是,即使我只需要同步结果,我也会调用 get_created_by_async_tasklet,然后在函数返回的未来调用 get_results。

这样做对性能、容易出错等有什么不便吗? 我发现对于每个查询、获取等始终使用 ndb 异步小任务要简单得多……并且仅在我需要时才调用其结果,或者执行更多异步操作,然后再调用 get_results。

希望这能得到很好的解释......

提前致谢!

【问题讨论】:

    标签: google-app-engine asynchronous google-cloud-datastore app-engine-ndb tasklet


    【解决方案1】:

    只使用异步调用并没有错,如果您想要同步结果,则立即使用 get_result。这正是 ndb 同步函数所做的:

    Here is an example from ndb.Key:

    def get(self, **ctx_options):
        """Synchronously get the entity for this Key.
    
        Return None if there is no such entity.
        """
        return self.get_async(**ctx_options).get_result()
    

    【讨论】:

    • 那么做所有的小任务呢?
    • 看起来很合理。你读过这个文件吗? developers.google.com/appengine/docs/python/ndb/async。它有一些很好的例子,看起来像你正在尝试做的事情。
    • 是的,我阅读了所有这些文档,但我不知道使用 tasklet 是否意味着性能或其他方面的任何缺陷。谢谢!
    • Patrick... 我发现 Query.get_async 在内部也使用了一个 tasklet。 @tasklets.tasklet def _get_async(self, **q_options): """Internal version of get_async()."""
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多