免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9302 | 回复: 6
打印 上一主题 下一主题

python 定时器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-21 23:57 |只看该作者 |倒序浏览
初始化一个定时器,希望在不用的时候可以删除,但是总是不行,代码如下,哪里不对呢~~~
from threading import Timer
from time import ctime, sleep

class foo:
    def __init__(self):
        pass
   
    def disconnect(self):
        self.timer.cancel()
        print('dead...')

    def keepAlive(self):
        print('aliving...')
        self.timer = Timer(1, self.keepAlive)
        self.timer.start()

f = foo()
f.keepAlive()
for i in range(3):
    print(i)
    sleep(1)
f.disconnect()

论坛徽章:
0
2 [报告]
发表于 2009-11-22 11:55 |只看该作者

回复 #1 markangelma 的帖子

同求这个问题,我之前曾在邮件列表中请教,limodou老大回复说  self.timer.cancel()并不能真正关闭该进程。
后来我一直没法解决 。。。  
郁闷中。。。

论坛徽章:
0
3 [报告]
发表于 2009-11-22 12:56 |只看该作者
感觉是...,可是我在执行定时器的过程中sleep一段时间后,在调用Timer.cancel()貌似可以,不知道又是怎么回事。如果用执行代码替换sleep语句,比如ftp长连接过程中是否可以。
from threading import Timer
from time import ctime, sleep

class foo:
    def __init__(self):
        pass
   
    def disconnect(self):
        self.timer.cancel()
        print('dead...')

    def keepAlive(self):
        print(ctime(), 'aliving...')
        self.timer = Timer(1, self.keepAlive)
        self.timer.start()

f = foo()
f.keepAlive()
sleep(9)
f.disconnect()

执行结果:
>>>
('Sun Nov 22 12:51:50 2009', 'aliving...')
('Sun Nov 22 12:51:51 2009', 'aliving...')
('Sun Nov 22 12:51:52 2009', 'aliving...')
('Sun Nov 22 12:51:53 2009', 'aliving...')
('Sun Nov 22 12:51:55 2009', 'aliving...')
('Sun Nov 22 12:51:56 2009', 'aliving...')
('Sun Nov 22 12:51:57 2009', 'aliving...')
('Sun Nov 22 12:51:58 2009', 'aliving...')
('Sun Nov 22 12:51:59 2009', 'aliving...')
dead...
>>>

论坛徽章:
0
4 [报告]
发表于 2009-11-22 14:16 |只看该作者
不要把self.timer定义在keepAlive函数中。

from python help:

cancel( )

    Stop the timer, and cancel the execution of the timer's action. This will only work if the timer is still in its waiting stage.

论坛徽章:
0
5 [报告]
发表于 2009-11-22 14:31 |只看该作者

回复 #4 nietsche 的帖子

不在函数体内定义的话,有什么办法能够得到返回值,用来调用cancel呢?

论坛徽章:
0
6 [报告]
发表于 2011-09-06 16:23 |只看该作者
from threading import Timer
from time import ctime, sleep

def timer_cb():
    global timer
    print(ctime(), 'aliving...')
    timer = Timer(0.5, timer_cb)  
    timer.start()
   
timer = Timer(0.5, timer_cb)  
timer.start()
sleep(3)
timer.cancel()
print('dead...')

我不用对象,就可以停止了,那个timer是全局变量,可能跟线程有关吧,全局变量能在线程中共享,
所以估计不能用self的。

论坛徽章:
0
7 [报告]
发表于 2011-09-07 10:44 |只看该作者
回复 1# markangelma
用不着cancle 只要停止生成新线程就行了,比如简单加个标志。
  1. class foo:
  2.     def __init__(self):
  3.         self.flag=True   
  4.     def disconnect(self):
  5.         self.flag=False
  6.         print('dead...')
  7.     def keepAlive(self):
  8.         if not self.flag:
  9.             return            
  10.         print('aliving...')
  11.         self.timer = Timer(1, self.keepAlive)
  12.         self.timer.start()
  13. f = foo()
  14. f.keepAlive()
  15. for i in range(3):
  16.     print(i)
  17.     sleep(1)
  18. f.disconnect()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP