Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to wait for Try Else Block execution with threading?

In the below code attached is there any way I can wait for else block print statement to get executed only after threading is done currently
as the function gg is threaded I am getting output

HelloNothing went wrong

Hello

Expected output

Hello
Hello
Nothing went wrong

Current code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import threading
import time 


def gg():
    print("Hello")
    time.sleep(5)
    print("Hello")

try:
    threading.Thread(target=gg).start()
except:
    print("Something went wrong")
else:
    print("Nothing went wrong")

>Solution :

You can use Join method

import threading
import time


def gg():
    print("Hello")
    time.sleep(5)
    print("Hello")


t1 = threading.Thread(target=gg)
try:
    t1.start()
    t1.join()
except:
    print("Something went wrong")
else:
    print("Nothing went wrong")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading