在 Python API在本例中为 Falcon中如何启动倒数计时器并在下一个请求中更新/取消

问题描述

我需要开始 60 秒倒计时,最后我想运行一个方法。如果另一个请求来了,那么我需要将计时器重置回 60 秒(重新启动计时器线程)

这是一个非常小的 API,我没有使用任何数据库。

我已经实现了类似下面的东西,它可以启动计时器,但是当我发送另一个请求时,它会启动另一个计时器,而不是因为新实例。知道如何在上一个请求中启动计时器线程的实例并取消/更新它吗?

    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()

发送

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)