代码之家  ›  专栏  ›  技术社区  ›  Philip

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

  •  1
  • Philip  · 技术社区  · 6 年前

    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() 从剧本里。这很管用。
    • 添加了 timeout=1 retry=False 到API调用。它仍然挂着。
    • 使用 requests 取而代之的是模块。这对芹菜很管用,所以问题是 LanguageServiceClient .

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Philip    6 年前

    因为问题似乎出在 LanguageServiceClient requests 模块来调用 celery 工人:

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