【问题标题】:Passing a cursor to ndb on Google App Engine Python 3 leads to an error在 Google App Engine Python 3 上将光标传递给 ndb 会导致错误
【发布时间】:2026-01-23 23:15:02
【问题描述】:

注意:这发生在我的开发服务器上(运行 Mojave 的 MAC)

我在 Google App Engine(标准环境)上运行 Python 3,我有以下代码

cursor = ndb.Cursor(urlsafe = next_page) if next_page else ndb.Cursor()
q = myObject.query(myObject.link == linkKey).order(-myObject.created)
resultsFuture = q.fetch_page_async(PAGE_SIZE,start_cursor=cursor)

如果 next_page 不是 None (这意味着 cursor 不是 None),我得到以下错误

Traceback (most recent call last):
  File .../env/lib/python3.7/site-packages/google/cloud/ndb/_datastore_api.py", line 92, in rpc_call
    result = yield rpc
grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
    status = StatusCode.INVALID_ARGUMENT
    details = "Error parsing protocol message"
    debug_error_string = "{"created":"<CREATION_TIME>","description":"Error received from peer ipv6:<MY_IP_ADDRESS>","file":"src/core/lib/surface/call.cc","file_line":1056,"grpc_message":"Error parsing protocol message","grpc_status":3}"
>

以前有没有人遇到过这种情况,如果有,解决办法是什么?

如果没有,谁能指出一个可能的解决方案?

【问题讨论】:

  • 我已将此作为问题添加到 NDB 问题跟踪器:github.com/googleapis/python-ndb/issues/471
  • 维护者有一些关于这个问题的问题
  • 我该如何应对?我在 Github 上没有帐户.. next_page 只是一个变量,我用来保存从上一次调用 fetch_page_async 返回的“光标”的值
  • 不用担心,我已经为您回复了。能否附上分配next_page 的代码?
  • 你能解决这个问题吗?我有同样的问题。我将它作为我的 HTML 页面中的 URL 传递

标签: google-app-engine google-cloud-platform app-engine-ndb google-app-engine-python


【解决方案1】:

https://github.com/googleapis/python-ndb/issues/471#issuecomment-649173225 说:

您好,fetch_page_async 返回的cursor 已经是Cursor 的一个实例。 Cursorurlsafe 参数是一个字符串,可以通过在游标实例上调用 Cursor.urlsafe() 来派生。由于fetch_page_async 返回的cursor 已经是Cursor 实例,因此可以直接将其传递给对fetch_page_async 的另一个调用。

所以你应该这样做:

cursor = next_page if next_page else ndb.Cursor()
q = myObject.query(myObject.link == linkKey).order(-myObject.created)
resultsFuture = q.fetch_page_async(PAGE_SIZE,start_cursor=cursor)

【讨论】:

  • 您好,有误会。是的,我知道 fetch_page 返回的游标已经是游标的一个实例。我将其转换为 urlsafe 字符串,然后将其传递给我的 html 页面,以便用户可以单击带有“下一页”标签的链接。当用户单击此链接时,它会作为变量“next_page”返回服务器,然后我必须将其转换回游标实例,然后尝试执行另一个 fetch_page_async,这会导致我报告的错误