Call to Google Cloud API in Celery task never returns

痴心易碎 提交于 2020-05-31 03:26:21

问题


I am trying to make a call to a external Google Cloud Natural Language API from within a Celery task (using the google-cloud-python package). The problem is that the call to the API never returns (hangs):

@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

What I've tried to solve problem:

  • Calling the method get_entities() from a script. This works fine.
  • Added a timeout=1 and retry=False to the API call. It still hangs.
  • Called the API using the requests module instead. This works fine with celery, so the problem has to be something within LanguageServiceClient.

Any ideas on how to debug or solve this problem?


回答1:


Since the problem seems to be in the LanguageServiceClient, I used the requests module instead to call the API inside the celery worker:

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())


来源:https://stackoverflow.com/questions/51964541/call-to-google-cloud-api-in-celery-task-never-returns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!