pythonwhile-loopboto3amazon-transcribe

How to check status for jobs in list each n seconds till all jobs finished?


I'm having a problem with checking the status for all my Transcription job before start another step. I need to know when all jobs in myList are COMPLTETED or FAILED, if it is false retry again 5 seconds later, but currently it keeps forever running, there is my python script:

while True:
    final_list = []
    for job in myList:
        status = transcribe.get_transcription_job(TranscriptionJobName=job)
        final_list.append(status.get('TranscriptionJob').get('TranscriptionJobStatus'))

    if all(status in final_list for status in ['COMPLETED', 'FAILED']):
        break
    time.sleep(5)

Edit 1 Possible response status can be QUEUED, IN_PROGRESS, FAILED, COMPLETED

Edit 2 Omar was right I'm not extracting corretly the status of each job response but script still running forever.


Solution

  • Response syntax of get_transcription_job method is:

    {
        'TranscriptionJob': {
            'TranscriptionJobName': 'string',
            'TranscriptionJobStatus': 'QUEUED'|'IN_PROGRESS'|'FAILED'|'COMPLETED',
            ...
        }
    }
    

    So instead of adding 'status' object which contains other fields, you need to append status.get('TranscriptionJob').get('TranscriptionJobStatus')

    Change:

    final_list.append(status)
    

    By:

    final_list.append(status.get('TranscriptionJob').get('TranscriptionJobStatus'))
    

    UPDATE:

    According to what are you trying to do, the condition should be:

    if all(status in ['COMPLETED', 'FAILED'] for status in final_list):
    

    Because you need to validate that allowed values list ('COMPLETED', 'FAILED') contains all the elements of 'final_list'.


    Reference:

    Boto3 get_transcription_job