GCP Firestore 上的 Python Time out of Stream 方法



我正在使用GCP firestore。出于某种原因,我正在查询集合中存在的所有文档。我正在使用python API。 我正在使用的代码

db=firestore.Client()
documents = db.collection(collection_name).stream()
for doc in tqdm(documents):
#some time consuming operation.(2-3 seconds)

一切运行正常,但 1 分钟后,for 循环结束。 我想也许连接超时了。我在文档页面上找到了这个。

The underlying stream of responses will time out after the max_rpc_timeout_millis value set in 
the GAPIC client configuration for the RunQuery API. Snapshots not consumed from the iterator 
before that point will be lost.

我的问题是我如何修改此超时值以满足我的需求。谢谢。

就我而言,Firestore 的503 The datastore operation timed out, or the data was temporarily unavailable.响应也引起了AttributeError: '_UnaryStreamMultiCallable' object has no attribute '_retry'

这看起来没有设置重试策略,尽管 Python 的firebase_admin包也能够重试超时错误。因此,我刚刚显式配置了一个基本的Retry对象,这解决了我的问题:

from google.api_core.retry import Retry
documents = db.collection(collection_name).stream(retry=Retry())

在我的例子中,190K 项目的集合在 5 分钟内导出。最初,迭代也在 60 秒后中断。

与直觉相反,如文档中所述,.stream()整个集合消耗的累积超时而不是单个项目或块检索。

因此,如果您的集合有 1000 个项目,并且每个项目处理需要 0.5 秒,则消耗时间的总和将达到 500 秒,这大于默认(未记录(超时 60 秒。

同样违反直觉的是,CollectionReference.stream方法的timeout参数不会覆盖文档中提到的max_rpc_timeout_millis。实际上,它的行为类似于客户端超时,并且操作在min(max_rpc_timeout_millis / 1000, timeout)秒后有效超时。

最新更新