googleapi客户端(Python):是否可以将BatchHttpRequest与ETag缓存一起使用

2024-05-19 19:17:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是YouTube数据API v3。在

是否可以生成一个大的BatchHttpRequest(例如,请参见here),并在httplib2级别使用etag进行本地缓存(例如,请参见here)?在

etag可以很好地处理单个查询,我不知道它们是否对批处理请求有用。在


Tags: 数据apihereyoutubev3级别etaghttplib2
1条回答
网友
1楼 · 发布于 2024-05-19 19:17:56

TL;DR

  • 缓存不能与BatchEquist一起使用

在这里

首先让我们看看初始化BatchHttpRequest:

from apiclient.http import BatchHttpRequest

def list_animals(request_id, response, exception):
  if exception is not None:
    # Do something with the exception
    pass
  else:
    # Do something with the response
    pass

def list_farmers(request_id, response):
  """Do something with the farmers list response."""
  pass

service = build('farm', 'v2')

batch = service.new_batch_http_request()

batch.add(service.animals().list(), callback=list_animals)
batch.add(service.farmers().list(), callback=list_farmers)


batch.execute(http=http)

第二步让我们看看ETags是如何使用的:

^{pr2}$

现在让我们分析:

观察BatchHttpRequest示例的最后一行:batch.execute(http=http),现在检查source code是否执行,它调用_refresh_and_apply_credentials,它应用我们传递给它的http对象。在

def _refresh_and_apply_credentials(self, request, http):
    """Refresh the credentials and apply to the request.
    Args:
      request: HttpRequest, the request.
      http: httplib2.Http, the global http object for the batch.
    """
    # For the credentials to refresh, but only once per refresh_token
    # If there is no http per the request then refresh the http passed in
    # via execute()

也就是说,接受http的execute调用可以通过您创建的ETag http来传递:

http = httplib2.Http(cache=memcache)
# This would mean we would get the ETags cached http
batch.execute(http=http)

更新1

也可以尝试使用自定义对象:

from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
from googleapiclient.discovery_cache.base import Cache
from googleapiclient.discovery_cache.file_cache import Cache as FileCache

custCache = FileCache(max_age=DISCOVERY_DOC_MAX_AGE)
http = httplib2.Http(cache=custCache)
# This would mean we would get the ETags cached http
batch.execute(http=http)

因为,这只是对http2 lib中评论的一种预感:

"""If 'cache' is a string then it is used as a directory name for
        a disk cache. Otherwise it must be an object that supports the
        same interface as FileCache.

结论更新2:

在再次验证googleapi python源代码之后,我发现,BatchHttpRequest是用'POST'请求修复的,内容类型是multipart/mixed;..-^{}。在

给出了一个线索,即BatchHttpRequest对于POST数据是有用的,而这些数据随后被处理。在

现在,记住这一点,观察^{}request方法使用的是:_updateCache仅当满足以下条件时:

  1. 请求在["GET", "HEAD"]或{}中,或者是redirect request
  2. 否则response.status in [200, 203] and method in ["GET", "HEAD"]
  3. if response.status == 304 and method == "GET"

这意味着,BatchHttpRequest不能与缓存一起使用。

相关问题 更多 >