How do I print the response of my get_transcription_job method, but only when it's complete?

141 Views Asked by At

I have a Lambda Function that starts a transcription job using a file uploaded to an s3 bucket. When the Lambda is triggered, the job is started using "start_transcription_job".

I then want to print the output of "get_transcription_job" in order to see its contents, but it's executed too quickly and it prints the response while 'TranscriptionJobStatus' = IN_PROGRESS.

I'd only like it to print the response when the status = completed. I tried using time.sleep(5) before the get_transcription_job method but the Lambda function times out.

2

There are 2 best solutions below

0
rlhagerm On

You can use a loop to keep checking the status, and exit the loop when the status is either Completed or Failed. There is a Boto3 example in the SDK Code Examples Library here that looks similar to what you could try:

max_tries = 60
    while max_tries > 0:
        max_tries -= 1
        job = transcribe_client.get_transcription_job(TranscriptionJobName=job_name)
        job_status = job['TranscriptionJob']['TranscriptionJobStatus']
        if job_status in ['COMPLETED', 'FAILED']:
            print(f"Job {job_name} is {job_status}.")
            if job_status == 'COMPLETED':
                print(
                    f"Download the transcript from\n"
                    f"\t{job['TranscriptionJob']['Transcript']['TranscriptFileUri']}.")
            break
        else:
            print(f"Waiting for {job_name}. Current status is {job_status}.")
        time.sleep(10)
0
Erik Erikson On

To respond to the completion of a transcription job, you create another Lambda function and subscribe it to the Transcribe service's EventBridge event[0]. In that response you can get the details of the job using get_transcription_job[1], and read from $job.Transcript.RedactedTranscriptFileUri or $job.Transcript.TranscriptFileUri as appropriate for your use case.

[0] https://docs.aws.amazon.com/transcribe/latest/dg/monitoring-events.html#job-event
[1] https://docs.aws.amazon.com/transcribe/latest/APIReference/API_GetTranscriptionJob.html