以下程序僅打印一次hello world,而不是每5秒打印一次字符串。定時器只在python中運行一次
from threading import Timer;
class TestTimer:
def __init__(self):
self.t1 = Timer(5.0, self.foo);
def startTimer(self):
self.t1.start();
def foo(self):
print("Hello, World!!!");
timer = TestTimer();
timer.startTimer();
(program - 1)
但是下面的程序每5秒打印一次字符串。
def foo():
print("World");
Timer(5.0, foo).start();
foo();
(program - 2)
爲什麼(程序-1)不會每5秒打印一次字符串?以及如何使(程序-1)每5秒連續打印一次字符串。
你爲什麼要在一個額外的類中包裝它以開始?這是必要的嗎? – nlsdfnbch