Python论坛  - 讨论区

标题:[python-chinese] Run a task every few seconds

2006年02月28日 星期二 11:25

wang dy dongyang.wang at gmail.com
Tue Feb 28 11:25:52 HKT 2006

想实现间隔若干时间运行某任务的功能, 看了很多资料最后找到这个,
大家看还有什么问题或更好的办法解决


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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 12:53

HoLin holin.he at gmail.com
Tue Feb 28 12:53:19 HKT 2006

用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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 13:02

wang dy dongyang.wang at gmail.com
Tue Feb 28 13:02:09 HKT 2006

timer之运行一次,不会loop
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/1ccdae3f/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 13:29

wolfg wolfg1969 at gmail.com
Tue Feb 28 13:29:03 HKT 2006

可以试试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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号