在Celery任务中调用Google Cloud API永远不会返回

问题描述 投票:1回答:1

我试图从Google Cloud Natural Language API任务(使用Celery包)中调用外部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。这适用于芹菜,所以问题必须在LanguageServiceClient内。

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

python flask google-cloud-platform celery
1个回答
0
投票

由于问题似乎在LanguageServiceClient中,我使用requests模块来调用celery工作者中的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())
© www.soinside.com 2019 - 2024. All rights reserved.