python如何实现程序定时执行的功能?

比如我写了一个小程序要求每隔5秒执行一次,要怎么实现呢?

sleep就可以吧,把程序作为一个线程,启动线程,里面加个sleep,示例如下:
import threading
import time
class Test(threading.Thread):
def __init__(self):
pass

def test(self):
print 'run test!'

def run(self):
while True:
print time.strftime('%Y-%m-%d %H:%M:%S')
self.test()
time.sleep(5)
#test...
a=Test()
a.run()
#...test
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答