编程语言
首页 > 编程语言> > python-如何正确理解“光标”

python-如何正确理解“光标”

作者:互联网

我正在尝试将光标应用到我的应用程序,但是文档对我来说不够清楚.
Google对光标的描述
http://code.google.com/appengine/docs/python/datastore/queries.html#Query_Cursors

The cursor’s position is defined as the location in the result list after the last result returned. A cursor is not a relative position in the list (it’s not an offset); it’s a marker to which the datastore can jump when starting an index scan for results. If the results for a query change between uses of a cursor, the query notices only changes that occur in results after the cursor. If a new result appears before the cursor’s position for the query, it will not be returned when the results after the cursor are fetched. Similarly, if an entity is no longer a result for a query but had appeared before the cursor, the results that appear after the cursor do not change. If the last result returned is removed from the result set, the cursor still knows how to locate the next result.

据我了解,查询结果似乎总是以默认顺序返回(例如__键__).然后,使用指定游标,它将添加一个过滤器以滤除此游标之前的所有结果.就像Google过去提到的那样.真的吗?

使用__键__和非唯一属性分页
http://code.google.com/appengine/articles/paging.html

另一个问题,游标可以与迭代或任务一起使用吗?
由于某些原因,此功能无法正常工作.
通常,它可能会在迭代过程中生成“找不到查询”.

这是我的示例:

people = Person.all().filter("age > ", 30)
if cursor:
     people.with_cursor(cursor)

try:
     for person in people: # query not found
        cursor = people.cursor()

except DeadlineExceededError:
     taskqueue.add(url="/people", params= {"cursor", cursor})

解决方法:

您的理解或多或少是正确的,但不能将光标视为仅添加过滤器.假设您首先按年龄,然后按名称对结果集进行了排序.如果您最后一次返回的结果是age = 30并且name = Bob,则没有一组条件可以准确地返回此后的结果-age> = 30,name> Bob不会返回31岁的爱丽丝.

游标更像是结果集中的书签.它表示您离开的地方,因此您可以稍后再回来.如果在光标之前或之后修改了结果集,则光标将停留在同一位置-因此您将始终从上次中断的地方开始.

回答您的其他问题:是的,查询始终具有隐含顺序.这取决于所查询的内容(在您的情况下,将首先按年龄,然后按键),但它可以确保对结果进行总排序.您引用的分页文章已过时,并且提供了分页前驱方法.您可以忽略它,而使用游标.

您可以在任务之间(以及与用户之间)传递光标.如果您看到错误,则必须向我们展示stacktrace,然后我们才能提供任何帮助.

标签:gql,google-app-engine,python
来源: https://codeday.me/bug/20191208/2089803.html