티스토리 뷰

Programming Languages/Python

Python Timer with Thread

sw편한세상 2011.11.20 01:08
import threading
import time

class ExTimer(threading.Thread):

	def __init__(self): 
		threading.Thread.__init__(self) 
		# default delay set.. 
		self.delay = 1
		self.state = True 
		self.handler = None

	def setDelay(self, delay): 
		self.delay = delay

	def run(self): 
		while self.state: 
				time.sleep( self.delay ) 
				if self.handler != None: 
						self.handler()

	def end(self): 
		self.state = False				   

	def setHandler(self, handler): 
		self.handler = handler

def timerHandler():
	print "Timer"

th = ExTimer()
th.setHandler(timerHandler)
th.setDelay(0.5)
th.start()



Python 매력이 넘치네~
댓글
댓글쓰기 폼