【问题标题】:Call to Google Cloud API in Celery task never returns在 Celery 任务中调用 Google Cloud API 永远不会返回
【发布时间】:2019-01-28 13:52:47
【问题描述】:

我正在尝试拨打外部Google Cloud Natural Language APICelery 任务中(使用google-cloud-python 包)。问题是对 API 的调用永远不会返回(挂起):

@celery.task()
def get_entities_async():
    return get_entities()

def get_entities():
    gcloud_client = LanguageServiceClient()
    doc = types.Document(content='This is a test.', language='en', type='PLAIN_TEXT')
    res = gcloud_client.analyze_entities(document=doc)  # This call never returns
    print('Call successful!')   # (This never gets printed)
    return res

我试图解决的问题:

  • 从脚本调用方法get_entities()。这很好用。
  • 向 API 调用添加了 timeout=1retry=False。它仍然挂起。
  • 改为使用requests 模块调用API。这适用于 celery,因此问题必须在 LanguageServiceClient 内。

关于如何调试或解决此问题的任何想法?

【问题讨论】:

  • Celery 有一个调试器 rdb 可以帮助您调查问题。

标签: python flask google-cloud-platform celery


【解决方案1】:

由于问题似乎出在LanguageServiceClient,因此我使用requests 模块来调用celery worker 内部的API:

import requests

# Temporary solution to call the Natural Language API
def get_entities():
    doc = {'type': 1, 'language': 'en', 'content': 'This is a test.'}
    d = {'document': doc, 'encodingType': 'UTF32'}
    url = 'https://language.googleapis.com/v1beta2/documents:analyzeEntities?key=' + API_KEY
    return requests.post(url, json=d, timeout=10.0).json())

【讨论】:

猜你喜欢
  • 2020-04-01
  • 2018-01-11
  • 1970-01-01
  • 2017-12-06
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多