2006年02月28日 星期二 11:25
想实现间隔若干时间运行某任务的功能, 看了很多资料最后找到这个, 大家看还有什么问题或更好的办法解决 import threading class TaskThread(threading.Thread): """Thread that executes a task every N seconds""" def __init__(self): threading.Thread.__init__(self) self._finished = threading.Event() self._interval = 15.0 def setInterval(self, interval): """Set the number of seconds we sleep between executing our task""" self._interval = interval def shutdown(self): """Stop this thread""" self._finished.set() def run(self): while 1: if self._finished.isSet(): return self.task() # sleep for interval or until shutdown self._finished.wait(self._interval) def task(self): """The task done by this thread - override in subclasses""" pass if __name__ == '__main__': class printTaskThread(TaskThread): def task(self): print 'running' tt = printTaskThread() tt.setInterval(2) print 'starting' tt.start() print 'started, wait now' import time time.sleep(7) print 'killing the thread' tt.shutdown() print 'killed and done' -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/bd315c85/attachment.htm
2006年02月28日 星期二 12:53
用timer不好吗? On 2/28/06, wang dy <dongyang.wang at gmail.com> wrote: > > 想实现间隔若干时间运行某任务的功能, 看了很多资料最后找到这个, > 大家看还有什么问题或更好的办法解决 > > > import threading > > > class TaskThread(threading.Thread): > """Thread that executes a task every N seconds""" > > def __init__(self): > threading.Thread.__init_ _(self) > self._finished = threading.Event() > self._interval = 15.0 > > def setInterval(self, interval): > """Set the number of seconds we sleep between executing our > task""" > self._interval = interval > > def shutdown(self): > """Stop this thread""" > self._finished.set() > > def run(self): > while 1: > if self._finished.isSet(): return > self.task() > > # sleep for interval or until shutdown > self._finished.wait(self._interval) > > def task(self): > """The task done by this thread - override in subclasses""" > pass > > if __name__ == '__main__': > class printTaskThread(TaskThread): > def task(self): > print 'running' > > tt = printTaskThread() > tt.setInterval(2) > print 'starting' > tt.start() > print 'started, wait now' > import time > time.sleep(7) > print 'killing the thread' > tt.shutdown() > print 'killed and done' > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/34482c6c/attachment.html
2006年02月28日 星期二 13:02
timer之运行一次,不会loop -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/1ccdae3f/attachment.htm
2006年02月28日 星期二 13:29
可以试试wxPython的wxTimer 在 06-2-28,wang dy<dongyang.wang at gmail.com> 写道: > timer之运行一次,不会loop > _______________________________________________ python-chinese Post: send python-chinese at lists.python.cn Subscribe: send subscribe to python-chinese-request at lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号