在 Celery 任务中调用 Google Cloud API 从不 returns

Call to Google Cloud API in Celery task never returns

我正在尝试拨打外部电话 Google Cloud Natural Language API 来自 Celery 任务(使用 google-cloud-python 包)。问题是对 API 的调用从不 returns (挂起):

@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

我尝试解决的问题:

关于如何调试或解决这个问题有什么想法吗?

由于问题似乎出在 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())