简体   繁体   English

Python如何查看一个Thread是否运行完毕?

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

How can I check whether a thread is completed is completed in Python?如何检查一个线程是否在 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.我找到了关于is_alive()这个方法,但是是找是否活着,不是找是否结束。 So it's gonna be great if someone can help me.所以如果有人能帮助我,那就太好了。 I'm using Python 3.10.我正在使用 Python 3.10。 Thank you!谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python Tornado中检查请求是否完成? - How to check whether request is finished in Python Tornado? 如何检查 SAP Server 是否与 python 一起运行 - How to check whether SAP Server is running with python 如何定期检查线程是否完成 - How to check periodically if a thread is finished 如何通过API检查交易是否完成? - How to check whether a transaction is finished or not through API? 如何检查由模块线程(在Python 3 _thread中)构成的线程是否正在运行? - How to check if thread made by module thread (in Python 3 _thread) is running? 如何使用python脚本启动其他python脚本并检查所有“子脚本”是否都已完成? - How to use python script to start other python scripts and check whether all “child scripts” are finished? 如何以编程方式检查apache tomcat是否在python中运行? - How to check programmatically whether apache tomcat is running or not in python? 如何检查浏览器是否正在使用python运行? - How do I check whether a browser is running using python? 在python中完成任务后如何停止线程? - How to stop a thread after the task is finished in python? Python 线程不活动/已完成运行但尚未返回值 - Python thread is not alive/finished running but value is not returned yet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM