[Python] python自定义线程类 →→→→→进入此内容的聊天室

来自 , 2021-04-04, 写在 Python, 查看 137 次.
URL http://www.code666.cn/view/709d00f2
  1. import threading  
  2. import time  
  3. class timer(threading.Thread): #The timer class is derived from the class threading.Thread  
  4.     def __init__(self, num, interval):  
  5.         threading.Thread.__init__(self)  
  6.         self.thread_num = num  
  7.         self.interval = interval  
  8.         self.thread_stop = False  
  9.    
  10.     def run(self): #Overwrite run() method, put what you want the thread do here  
  11.         while not self.thread_stop:  
  12.             print 'Thread Object(%d), Time:%s\n' %(self.thread_num, time.ctime())  
  13.             time.sleep(self.interval)  
  14.     def stop(self):  
  15.         self.thread_stop = True  
  16.          
  17.    
  18. def test():  
  19.     thread1 = timer(1, 1)  
  20.     thread2 = timer(2, 2)  
  21.     thread1.start()  
  22.     thread2.start()  
  23.     time.sleep(10)  
  24.     thread1.stop()  
  25.     thread2.stop()  
  26.     return  
  27.    
  28. if __name__ == '__main__':  
  29.     test()  
  30. #//python/8232

回复 "python自定义线程类"

这儿你可以回复上面这条便签

captcha