获取python类函数在tim之后的返回值

2024-10-02 02:26:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的python类Timer_u,它以秒为单位,在后台运行计时器。完成后,它运行一个返回true的finished函数。当计时器以变量的形式停止时,如何获得这个函数的值?谢谢您!在

import threading



class Timer_():

    def __init__(self, interval):
        #interval in seconds
        self.interval = interval
        self.finished = False

    def run(self):
        self.timel = threading.Timer(float(self.interval), self.finish)
        self.timel.start()

    def finish(self):
        self.finished = True
        return True

    def cancel(self):
        self.timel.cancel()



time = Timer_(5)
time.run()
print(time.finished)

Tags: 函数runselftruetimedef单位cancel
1条回答
网友
1楼 · 发布于 2024-10-02 02:26:48

听起来你想等到time.finishedTrue

尝试对time.finished的值使用while循环:

time = Timer_(5)
timer.run()
while not timer.finished:
    pass
x = timer.finished
print(x)

这将在while中循环,直到timer.finishedTrue,然后您可以将该值用作变量或继续。在

相关问题 更多 >

    热门问题