简体   繁体   English

Python中的Google Cloud Datastore超时错误

[英]Google Cloud Datastore timeout errors in python

I have a table in Google Cloud Datastore where I store a small data structure which is written in one Python service and read in another. 我在Google Cloud Datastore中有一个表,该表中存储了一个小的数据结构,该结构在一个Python服务中编写,在另一个Python服务中读取。 I am using gcloud version 0.15.0. 我正在使用gcloud版本0.15.0。 Here is the Python code that I use to write/read data to/from GCD: 这是我用来向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'])

I am observing a large number of read/write failures with the message "The read operation timed out"; 我正在通过消息“读取操作超时”观察到大量读取/写入失败; >5% failures which is quite contrary to the documentation that mentions an expected failure rate of 1 in 30K. > 5%的故障,这与文档中提到的预期故障率每30K为1完全相反。

My questions then are: 我的问题是:

  1. Is it possible to increase timeout in the datastore.client.get and datastore.client.put calls? 是否有可能增加datastore.client.get和datastore.client.put调用中的超时? I am not looking for answers based on retries; 我不是在寻找基于重试的答案; already tried and don't want to depend just on retries. 已经尝试过,不想只依赖重试。

  2. Is there anything I should do when creating the table or setting up the client that can mitigate these timeout errors? 创建表或设置客户端时,我应该做些什么来减轻这些超时错误吗?

  3. I read somewhere ( https://github.com/GoogleCloudPlatform/gcloud-python/issues/1214 ) that the Python gcloud uses httplib2.Http which is not threadsafe and has timeout issues. 我在某处( https://github.com/GoogleCloudPlatform/gcloud-python/issues/1214 )看到Python gcloud使用httplib2.Http,它不是线程安全的,并且存在超时问题。 Is there a way to use the (more stable) Python requests package? 有没有办法使用(更稳定的)Python请求包?

Thanks, 谢谢,

You can use requests or urllib3 without any problems, see https://google-auth.readthedocs.io/en/latest/user-guide.html#making-authenticated-requests 您可以urllib3使用requestsurllib3 ,请参阅https://google-auth.readthedocs.io/en/latest/user-guide.html#making-authenticated-requests

AFAIK the requests default timeout is None, so it waits infinitely (until the server closes it.) You can can pass own Session to the AuthorizedSession also, which override the request method and sets the default timeout as you like. AFAIK请求的默认超时为None,因此它将无限期等待(直到服务器关闭它。)您也可以将自己的Session传递给AuthorizedSession ,后者可以覆盖request方法并根据需要设置默认超时。

If you still experience the problem, then I recommend some retry mechanism :-) Wath the issue https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2694 如果您仍然遇到问题,那么我建议您使用一些重试机制:-)问题https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2694

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM