【问题标题】:How do I query GCP Search APIs asynchronously from an App Engine instance?如何从 App Engine 实例异步查询 GCP Search API?
【发布时间】:2019-04-14 21:27:30
【问题描述】:

我需要使用 Search API 搜索我的 App Engine 索引文档。据我所知,搜索 API 只能通过标准环境的 google.appengine API 引用。

我的问题是一些补水、冲洗和查询需要超过 60 秒。我需要从应用引擎返回响应,继续在后台处理请求,然后将结果发布到 Pub/Sub。但是,我可以在标准环境中生成线程或使用 background_thread。我会切换到灵活环境,但我无法使用 Python Search API 库。

切换到 flex 环境并使用 REST API 是我唯一的选择吗?

【问题讨论】:

  • 你试过index.search_async()吗?
  • 我认为这行不通。假设我必须删除 16,000 个文档,然后由于 15,000 次操作/分钟的限制,我需要在 15,000 个之后暂停我的代码一分钟,然后再处理剩余的文档。
  • 无论你使用哪种环境,Search中的删除操作限制总是15000/min,你可以做的是联系google升级它或建立删除作业到云任务(独立工作和仍然有 15000 个限制)并将数据发布到 Pub/Sub first。如果第二个可以接受,我可以分享一个例子。
  • 当然!你介意分享一个例子吗?

标签: google-app-engine google-cloud-platform google-search-api google-app-engine-python app-engine-flexible


【解决方案1】:

您可能想要使用应用引擎任务队列,这是一个任务调度程序,用于将待处理的任务排队等待另一个应用引擎来完成,因为应用引擎是单线程引擎。

例如,

1.设置新服务来处理任务(可选)

设置一个类似于您的app.yaml 的 yaml 调用 newtaskworker.yaml,因为您可能需要其他服务来完成该任务,而不是原来的服务。

唯一的区别是记得为它添加一个服务名称,service: newtaskworker

记得通过gcloud app deploy newtaskworker.yaml部署

2。设置队列

阅读How to Create new queue,一般来说你需要一个queue.yaml 来对任务进行排队。记得通过gcloud app deploy queue.yaml部署它

queue:
- name: queue_name
  rate: 20/s #You may limit the speed of *START* new task here
  bucket_size: 40
  max_concurrent_requests: 10 #This is limited by your max_instances allowed in newtaskworker.yaml, you may simply omit it

3.最后是你的代码

from google.appengine.api import taskqueue

#/deleteTask
class DeleteTask(webapp2.RequestHandler):
    def get(self):
        paramA      = self.request.get('paramA')
        paramB      = self.request.get('paramB')
        #your Search delete here

class GetPostDataTask(webapp2.RequestHandler):
    def get(self):   
        #If you don't want to use a new service, simply use 'default` in target.
        #Your Go to Pub/Sub work here.
        taskqueue.add(queue_name='queue_name', url='/deleteTask', method='GET', params={'paramA': 1, 'paramB': 2}, target='newtaskworker')

如果没有问题,您可以在控制台 -> 工具 -> 云任务中找到您的任务

【讨论】:

  • 哇。我认为这解决了我的问题。明天我试了之后告诉你。谢谢!!!
猜你喜欢
  • 1970-01-01
  • 2022-07-26
  • 2019-04-30
  • 2020-10-23
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多