问题
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
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?
回答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