2017-05-12 3 views
0
아래

은 '이 이상적으로 돈을 잠시 내가 걸릴 수 있습니다, 그러나장기간 실행되는 Google Cloud Speech API 작업의 결과를 나중에 얻는 방법은 무엇입니까? 이 장기 실행 작업이 그대로

from google.cloud import speech 
speech_client = speech.Client() 

audio_sample = speech_client.sample(
    content=None, 
    source_uri=gcs_uri, 
    encoding='FLAC', 
    sample_rate_hertz=44100) 

operation = audio_sample.long_running_recognize('en-US') 

retry_count = 100 
while retry_count > 0 and not operation.complete: 
    retry_count -= 1 
    time.sleep(60) 
    operation.poll() 

을 텍스트로 오디오 파일을 변환 할 Google 클라우드 음성 API 장기 실행 작업을 호출하는 코드 조각입니다 기다리는 동안 세션을 계속 유지하려고하지 마십시오. 정보를 저장하고 결과를 나중에 검색 할 수 있습니까?

답변

1

소스를 읽은 후 GRPC의 시간 제한이 10 분임을 알게되었습니다. 큰 파일을 제출하는 경우 10 분 이상 걸릴 수 있습니다. 트릭은 HTTP 백엔드를 사용하는 것입니다. HTTP 백엔드는 GRPC와 같은 연결을 유지하지 않고 대신 폴링 할 때마다 HTTP 요청을 보냅니다. 다른 답변에서 언급 한 바와 같이 HTTP를 사용하려면,

speech_client = speech.Client(_use_grpc=False)

0

아니요, 그렇게 할 방법이 없습니다. 스레드 모듈을 사용하면 다음 작업을 실행할 때 백그라운드에서 실행할 수 있습니다.

0

을, 당신은 메인 스레드가 계속하면서 작업을 폴링하는 별도의 스레드를 사용할 수 있습니다. 또는 반환 된 작업의 operation.name을 별도의 서비스에 전달하고 다른 서비스에서 폴링을 처리하도록 할 수 있습니다. 실제로 장기 실행 작업을 호출하는 서비스는 예를 들어 Pub/Sub 항목에 operation.name을 게시 할 수 있습니다.

{u'done': True, 
u'metadata': {u'@type': u'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata', 
    u'lastUpdateTime': u'2017-06-21T19:38:14.522280Z', 
    u'progressPercent': 100, 
    u'startTime': u'2017-06-21T19:38:13.599009Z'}, 
u'name': u'...................', 
u'response': {u'@type': u'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse', 
    u'results': [{u'alternatives': [{u'confidence': 0.987629, 
     u'transcript': u'how old is the Brooklyn Bridge'}]}]}} 
: 작업이 완료되면

from oauth2client.client import GoogleCredentials 
from googleapiclient import discovery 

credentials = GoogleCredentials.get_application_default() 
speech_service = discovery.build('speech', 'v1', credentials=credentials) 

operation_name = .... # operation.name 

get_operation_request = speech_service.operations().get(name=operation_name) 

# response is a dictionary 
response = get_operation_response.execute() 

# handle polling 
retry_count = 100 
while retry_count > 0 and not response.get('done', False): 
    retry_count -= 1 
    time.sleep(60) 
    response = get_operation_response.execute() 

하기는 response DICT는 다음과 같은 형태가 될 것이다 : 아래

이름으로 그것을보고 긴 실행 작업을 검색하는 가능한 방법입니다