educative.io

Educative

Printing even and odd numbers using 2 separate threads

I am trying to write a code for printing even and odd numbers using threads in python. I have written the below code:

from threading import Thread
from threading import Event


even_available = Event()
odd_available = Event()
    
def print_even():
    global limit
    global exit_prog
    global number
    
    while not exit_prog and number <= limit:
        print(number)
        number = number + 1
        even_available.clear()

        odd_available.set()

def print_odd():
    global limit
    global exit_prog
    global number
    
    odd_available.wait()
    while not exit_prog and number <= limit:
        print(number)
        number = number+1
        odd_available.clear()
        even_available.set()

if __name__ == "__main__":
    
    limit = 10
    exit_prog = False
    number = 0

    t1 = Thread(target=print_even)
    t2 = Thread(target=print_odd)
    
    even_available.set()

    t1.start()
    t2.start()

    t1.join()
    t2.join()

But this code isn’t working. Can anybody help in understanding why it’s not working?