解决在使用计时器同时进行数据库更新的问题

22 阅读4分钟

我正在编写一个python脚本,在使用time.sleep函数的同时,将数据写入sqlite3数据库。

  • 当我在使用time.sleep函数时,遇到了在数据库中写入数据的问题。
  • 我能够创建数据库表没有任何问题,但是当我使用time.sleep函数时,它将停止向数据库中写入数据。
  • 当我使用以下代码时:
import threading

#DOWNLOAD THE XML SOURCE HERE
url = ADDON.getSetting('allchannel.url')
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
response.close()
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide',''))
self.getControl(4202).setLabel("1%")


if os.path.exists(profilePath):
   profilePath = profilePath + 'source.db'
   con = database.connect(profilePath)
   cur = con.cursor()
   cur.execute('CREATE TABLE programs(channel TEXT, title TEXT, start_date TIMESTAMP, stop_date TIMESTAMP, description TEXT)')
   con.commit()
   con.close
   tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()
   profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
   profilePath = profilePath + 'source.db'
   con = sqlite3.connect(profilePath)
   cur = con.cursor()
   channels = OrderedDict()

   # 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%")

  • 它会允许我在不使用time.sleep函数的情况下将数据写入数据库。我想使用time.sleep函数或计时器,因为我想在写入数据到数据库的同时,每2秒更新setLabel函数中的字符串。
  • 我尝试过使用while循环,它会冻结我正在运行的应用程序。

*当我运行threading时,我得到了太多的打印:

15:39:51 T:2104  NOTICE: hello, world
15:39:51 T:6924  NOTICE: hello, world
15:39:51 T:6052  NOTICE: hello, world
15:39:51 T:1696  NOTICE: hello, world
15:39:51 T:6164  NOTICE: hello, world
15:39:51 T:1312  NOTICE: hello, world
15:39:51 T:3804  NOTICE: hello, world
15:39:51 T:6364  NOTICE: hello, world
15:39:51 T:4208  NOTICE: hello, world
15:39:51 T:3332  NOTICE: hello, world
15:39:51 T:5428  NOTICE: hello, world
15:39:51 T:920  NOTICE: hello, world
15:39:51 T:6408  NOTICE: hello, world
15:39:51 T:7000  NOTICE: hello, world
15:39:51 T:1988  NOTICE: hello, world
15:39:51 T:5544  NOTICE: hello, world
15:39:51 T:6620  NOTICE: hello, world
15:39:51 T:6448  NOTICE: hello, world
15:39:51 T:1536  NOTICE: hello, world
15:39:51 T:1132  NOTICE: hello, world
15:39:51 T:6548  NOTICE: hello, world
15:39:51 T:1892  NOTICE: hello, world
15:39:51 T:6532  NOTICE: hello, world
15:39:51 T:3856  NOTICE: hello, world
15:39:51 T:788  NOTICE: hello, world
15:39:51 T:6416  NOTICE: hello, world
15:39:51 T:5692  NOTICE: hello, world
15:39:51 T:5256  NOTICE: hello, world
15:39:51 T:6696  NOTICE: hello, world
15:39:51 T:1352  NOTICE: hello, world
15:39:51 T:6656  NOTICE: hello, world
15:39:51 T:4844  NOTICE: hello, world
15:39:51 T:4672  NOTICE: hello, world
15:39:51 T:3636  NOTICE: hello, world
15:39:51 T:7052  NOTICE: hello, world
15:39:51 T:6264  NOTICE: hello, world
15:39:51 T:6336  NOTICE: hello, world
15:39:51 T:6332  NOTICE: hello, world
15:39:51 T:7064  NOTICE: hello, world
15:39:51 T:7148  NOTICE: hello, world
15:39:51 T:6984  NOTICE: hello, world
15:39:51 T:4924  NOTICE: hello, world
15:39:51 T:5716  NOTICE: hello, world
15:39:51 T:6960  NOTICE: hello, world
15:39:51 T:1828  NOTICE: hello, world
15:39:51 T:5492  NOTICE: hello, world
15:39:51 T:6560  NOTICE: hello, world
15:39:51 T:3328  NOTICE: hello, world
15:39:51 T:6880  NOTICE: hello, world
15:39:51 T:6152  NOTICE: hello, world
15:39:51 T:6892  NOTICE: hello, world
15:39:51 T:5048  NOTICE: hello, world
15:39:51 T:6788  NOTICE: hello, world
15:39:51 T:6168  NOTICE: hello, world
15:39:51 T:4656  NOTICE: hello, world
15:39:51 T:6392  NOTICE: hello, world
15:39:51 T:6928  NOTICE: hello, world
15:39:52 T:6240  NOTICE: hello, world
15:39:52 T:5240  NOTICE: hello, world
15:39:52 T:6824  NOTICE: hello, world
15:39:52 T:6716  NOTICE: hello, world
15:39:52 T:5384  NOTICE: hello, world
15:39: