在Celery任务中调用Google Cloud API永远不会返回 [英] Call to Google Cloud API in Celery task never returns
本文介绍了在Celery任务中调用Google Cloud API永远不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试拨打外部Google Cloud Natural Language API
从Celery
任务(使用google-cloud-python
包)中.问题在于对API的调用永远不会返回(挂起):
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
我试图解决的问题:
- 从脚本中调用方法
get_entities()
.效果很好. - 在API调用中添加了
timeout=1
和retry=False
.它仍然挂起. - 改为使用
requests
模块调用API.芹菜可以很好地工作,所以问题必须出在LanguageServiceClient
内.
- Calling the method
get_entities()
from a script. This works fine. - Added a
timeout=1
andretry=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 withinLanguageServiceClient
.
关于如何调试或解决此问题的任何想法?
Any ideas on how to debug or solve this problem?
推荐答案
由于问题似乎出在LanguageServiceClient
中,因此我使用requests
模块而不是在celery
worker中调用API:>
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())
这篇关于在Celery任务中调用Google Cloud API永远不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文