如何从App Engine实例异步查询GCP搜索API?

时间:2018-11-12 07:16:34

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

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

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

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

1 个答案:

答案 0 :(得分:1)

您可能要使用App Engine任务队列,它是一个任务调度程序,用于排队待处理的任务供另一个App Engine执行,因为App Engine是单线程引擎。

例如,

1。设置新服务以处理任务(可选)

设置一个Yaml调用newtaskworker.yaml,它与您的app.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')

如果一切正常,您可以在控制台->工具->云任务中找到您的任务