python 循环定时器

有时候需要循环执行某个任务,最简单的就是用thread.Timer.

谷歌了一下,发现大家竟然用sleep 来实现循环,也不知道谁想的这个方法,竟然很少有人想到join一下,很奇怪。

# -*- coding: utf-8 -*-
'''
Created on 2016年4月25日

@author: 55Haitao
'''

import threading

class Person(object):
    def __init__(self):
        print "init person"
        
    def speak(self):
        print "speak"
        



if __name__ == "__main__":
    p = Person()
    while True:
        timer = threading.Timer(5, Person.speak, (p,))
        print "start"
        timer.start()
        timer.join()
        print "after join"
    
    
    
    
    
    
        
        

  

原文地址:https://www.cnblogs.com/suyuan1573/p/5429940.html