【问题标题】:Google Cloud Datastore timeout errors in pythonpython中的Google Cloud Datastore超时错误
【发布时间】:2016-11-16 15:41:59
【问题描述】:

我在 Google Cloud Datastore 中有一个表,我在其中存储了一个小型数据结构,该结构用一个 Python 服务编写并在另一个 Python 服务中读取。我正在使用 gcloud 版本 0.15.0。这是我用来向 GCD 写入/读取数据的 Python 代码:

from gcloud import datastore
import datetime
import json
class GCD(object):
def __init__(self, project_id):
    self.client = datastore.Client(project_id)

def put(self, table, key, data):
    with self.client.transaction():
        entity = datastore.Entity(self.client.key(table, key), exclude_from_indexes=['context'])
        entity.update({'context': json.dumps(data), 'created': datetime.datetime.utcnow(), 'done': True})
        try:
            self.client.put(entity)
        except Exception as e:
            print "GCD save failed with exception: %s" % e
    return None

def get(self, table, key):
    entity_key = self.client.key(table, key)
    entity = None
    try:
        entity = self.client.get(entity_key)
    except Exception as e:
        print "GCD read failed with exception: %s" % e
    if not entity:
        return None
    else:
        return json.loads(entity['context'])

我观察到大量读/写失败,并显示“读操作超时”消息; >5% 的失败,这与文档中提到的预期失败率为 30K 中的 1 是完全相反的。

那么我的问题是:

  1. 是否可以增加 datastore.client.get 和 datastore.client.put 调用的超时时间?我不是根据重试来寻找答案;已经尝试过,不想仅仅依赖重试。

  2. 在创建表或设置客户端时我应该做些什么来缓解这些超时错误?

  3. 我在某处 (https://github.com/GoogleCloudPlatform/gcloud-python/issues/1214) 读到 Python gcloud 使用的 httplib2.Http 不是线程安全的并且存在超时问题。有没有办法使用(更稳定的)Python requests 包?

谢谢,

【问题讨论】:

  • 调用 put() 和 get() 的频率大约是多少?

标签: python google-cloud-datastore google-app-engine-python gcloud-python google-cloud-python


【解决方案1】:

您可以使用requestsurllib3 没有任何问题,请参阅https://google-auth.readthedocs.io/en/latest/user-guide.html#making-authenticated-requests

AFAIK 请求默认超时为无,因此它会无限等待(直到服务器关闭它。)您也可以将自己的Session 传递给AuthorizedSession,它会覆盖request 方法并设置默认超时随你喜欢。

如果您仍然遇到问题,那么我推荐一些重试机制 :-) Wath the issue https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2694

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 2014-01-26
    • 2016-12-27
    • 2021-06-28
    • 2021-10-31
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    相关资源
    最近更新 更多