如何在python中创建计时器

时间:2014-06-21 21:36:18

标签: python python-2.7 xbmc

我正在处理我的python脚本,因为我想每2秒设置一次计时器,以允许我更新setLabel函数中的值。我需要为setLabel创建每个不同值的计时器,因为它们只允许我一次更新一次。

这是例如:

#set the timer for 2 seconds to update the value in the setlabel
self.getControl(4202).setLabel("1%")


#Stop the timer and set the timer again for 2 seconds
self.getControl(4202).setLabel("8%")


#Stop the timer and set the timer again for another 2 seconds
self.getControl(4202).setLabel("16%")

依旧......

您能否告诉我如何创建定时器以允许我一次更新每个定时器的值?

编辑:当我尝试这样做时:

# Get the loaded data
                         for channel in tv_elem.findall('channel'):
                             channel_name = 
channel.find('display-name').text
                             for program in channel.findall('programme'):
                                 title = program.find('title').text
                                 start_time = program.get("start")
                                 stop_time = program.get("stop")
                                 cur.execute("INSERT INTO programs(channel, 
title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, 
title, start_time, stop_time])
                                 con.commit()
                                 con.close

                                 time.sleep(2)
                                 #Stop the timer and set the timer again 
for 2 seconds
                                 self.getControl(4202).setLabel("8%")

                                 time.sleep(2)
                                 #Stop the timer and set the timer again 
for another 2 seconds
                                 self.getControl(4202).setLabel("16%")

                                 time.sleep(2)
                                 #Stop the timer and set the timer again 
for another 2 seconds
                                 self.getControl(4202).setLabel("24%")

它不会让我在数据库中写入数据。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您是否考虑过sleep模块中的time

import time

time.sleep(2) # probably don't need this one
#set the timer for 2 seconds to update the value in the setlabel
self.getControl(4202).setLabel("1%")


time.sleep(2)
#Stop the timer and set the timer again for 2 seconds
self.getControl(4202).setLabel("8%")


time.sleep(2)
#Stop the timer and set the timer again for another 2 seconds
self.getControl(4202).setLabel("16%")

答案 1 :(得分:0)

有一些Python调度库可供选择。 Celery是一个非常强大的同步任务队列和消息系统,支持计划任务。

您是否尝试过Advanced Python Scheduler(APScheduler) 我喜欢。你可以调用函数

@sched.interval_schedule(minutes=3)
def timed_job():
    print 'This job is run every three minutes.'