Abhimanyu Sharma
Abhimanyu Sharma

Reputation: 903

How To Join These Threads

from threading import Thread
from time import sleep
def run1():
    for i in range(5):
        print("hello")
        sleep(1)
def run2():
    for i in range(5):
        print("hi")
        sleep(1)
if __name__ == "__main__":
    Thread(target=run1).start()
    Thread(target=run2).start()
    print("Bye !")

i threaded two functions run1 and run2 why is bye printing in between of these threads i already tried joining them but it doesn't work

Upvotes: 0

Views: 33

Answers (1)

freakish
freakish

Reputation: 56467

This is the code you are most likely looking for:

if __name__ == "__main__":
    th1 = Thread(target=run1)
    th1.start()
    th2 = Thread(target=run2)
    th2.start()
    th1.join()
    th2.join()
    print("Bye !")

Upvotes: 1

Related Questions