简体   繁体   中英

How to check whether a Thread is finished running in Python?

How can I check whether a thread is completed is completed in Python? I have this code:

async def transcribe():
    # Initializes the Deepgram SDK
    global response

    dg_client = Deepgram(DEEPGRAM_API_KEY)

    global filename
    global PATH_TO_FILE
    PATH_TO_FILE = filename

    with open(filename, 'rb') as audio:
        source = {'buffer': audio, 'mimetype': MIMETYPE}
        options = {'punctuate': True, 'language': 'en-US'}

        print('Requesting transcript...')
        print('Your file may take up to a couple minutes to process.')
        print('While you wait, did you know that Deepgram accepts over 40 audio file formats? Even MP4s.')
        print('To learn more about customizing your transcripts check out developers.deepgram.com')

        response = await dg_client.transcription.prerecorded(source, options)
        print(response)
        print(response['results']['channels'][0]['alternatives'][0]['transcript'])
        
def runTranscribe():
    asyncio.run(transcribe())

thread = threading.Thread(
    target = runTranscribe
)

I found about the is_alive() method, but it is to find whether it is alive, not to find whether it is finished. So it's gonna be great if someone can help me. I'm using Python 3.10. Thank you!

while True:
    if thread.is_alive():
        pass
    else:
        #do something
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM