Hi all,

I need your help, I'm having a trouble with writing the database at the same time when I'm using the time.sleep function.

I can be able to create the database table without have any problem, but when I use the time.sleep function, it will stop writing the data into the database.

When I use this code:

import time
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%")

So when I try this without using the time.sleep function:

   # 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

It will allow me to write the data into the database if I use without the time.sleep function. I want to use the time.sleep function or the timer, because I want to update the string in the setLabel function in every 2 seconds while I'm writing the data into the database. I tried to use the while loop, it will freeze the application that I'm running on.

Can you please tell me how I can write the data into the database at the same time when I'm using the time.sleep or the timer function?

time.sleep() stops the thread, so all other operations inside are paused.

If you literally want to do both things at once, you'll need to use multi-threading. You'll use one thread for updating the information to the gui, and you'll use the other thread to update the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.