0

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!

JKR
  • 69
  • 1
  • 1
  • 10
  • Your code is kinda wrong (indent), can you update it? – BrainFl Apr 05 '22 at 16:03
  • Thanks for informing. I hope that's OK now. – JKR Apr 05 '22 at 16:04
  • The async code is done when `asyncio.run()` returns. However, since you're creating threads, this is not really an async question, but about threading? – MatsLindh Apr 05 '22 at 16:09
  • Does this answer your question? [Wait for async function to complete](https://stackoverflow.com/questions/57234827/wait-for-async-function-to-complete) – alex Apr 05 '22 at 16:11
  • @MatsLindh You're right, threading can solve this one! But I'm not sure how. Will update the question anyways – JKR Apr 05 '22 at 16:31
  • 1
    My point is that you _are_ using threading. Your `asyncio.run()` method will terminate when the async task is done. However, remember that async does not mean parallel. – MatsLindh Apr 05 '22 at 17:19
  • What do you want the is_alive method to do that it doesn't do already? The docs say: "This method [`is_alive`] returns True just before the run() method starts until just after the run() method terminates." I don't know what more you can ask for. – Paul Cornelius Apr 06 '22 at 02:19
  • Oh, I tried it and it runs as the same way you said PaulCornelius. And also @MatsLindh, what is the solution that you think would solve this problem, then? – JKR Apr 06 '22 at 02:40
  • It depends on what you actually want to achieve, but when `asyncio.run(transcribe())` returns, the task is finished - so you should be able to just launch your next task after that if you want to invoke them in sequence. – MatsLindh Apr 06 '22 at 07:53

1 Answers1

0
while True:
    if thread.is_alive():
        pass
    else:
        #do something
        break
RIXDOT
  • 1
  • 2