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"
    
    
    
    
    
    
        
        

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM