Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

multithreading - In Python API (Falcon in this case) how to start a countdown timer and update/cancel in next request

I need to start a 60s countdown and at the end I want to run a method. And if another request comes, then I need to reset the timer back to 60s (restart the timer thread)

This is a very small API and I am not using any database.

I have implemented something like below, it works in starting the timer but when I send another request it start another timer instead ofcourse because of new instance. Any idea how to get instance of timer thread started in previous request and cancel/update it?

    counter = 0
    def start_timer(self, interval=1,seconds=CONFIG_FETCH_WAIT_TIMER):
        """Starts the 60 seconds timer and after that will call gather data and dump config files 

        Args:
            interval (int, optional): [description]. Defaults to 1.
            seconds (int, optional): [description]. Defaults to 60.
        """
        timer = threading.Timer(interval, self.start_timer)
        self.counter += interval

        for thread in threading.enumerate(): 
            if thread.name != "MainThread":
                thread.cancel()
        
        timer.start()
        print(self.counter)
        if self.counter == seconds:
            print("Timer Reached")
            timer.cancel()
            self.__start_doing_important_stuff()

Tx

question from:https://stackoverflow.com/questions/65599709/in-python-api-falcon-in-this-case-how-to-start-a-countdown-timer-and-update-ca

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...