'how do I stop this while loop after a specific time?

I'm currently making a lightning code, but I have some problems with the while loop. I want to stop the While loop after a specific time that I set. here's the code.

class Light:
    def __init__(self):
        self.work_flag = False

    def start(self):
        self.work_flag = True

    def stop(self):
        self.work_flag = False


    def mode_1(self):
        print("turn on red+green+blue light")
        sleep(0.5)
        while self.work_flag:
            print("turn on red light")
            sleep(0.3)
            print("turn on green light")
            sleep(0.3)
            print("turn on blue light")
            sleep(0.3)
            print("turn on red+green light")
            sleep(0.3)
            print("turn on red+blue light")
            sleep(0.3)
            print("turn on green+blue light")
            sleep(0.3)

light = Light()

def light_start():
    light.start()
    light.mode_1()

def light_stop():
    light.stop()

light_start()
sleep(5)
light_stop()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source