我正在使用python脚本在使用time.sleep函数的同时在sqlite3数据库中写入数据。我在使用time.sleep函数的同时编写数据库时遇到了麻烦。
我可以创建数据库表而不会有任何问题,但是当我使用time.sleep函数时,它将停止将数据写入数据库。
当我使用此代码时:
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%")
因此,当我在不使用time.sleep函数的情况下尝试此操作时:
# 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函数的情况下使用它,它将允许我将数据写入数据库。我想使用time.sleep函数或计时器,因为我想在每2秒钟更新setLabel函数中的字符串,同时我将数据写入数据库。我尝试使用while循环,它将冻结我正在运行的应用程序。
您能否告诉我如何在使用time.sleep或计时器功能的同时将数据写入数据库?
编辑:运行线程时我得到的打印太多了:
15:39:51 T:6980 NOTICE: hello, world
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:52 T:6008 NOTICE: hello, world
15:39:52 T:2280 NOTICE: hello, world
15:39:52 T:2612 NOTICE: hello, world
15:39:52 T:6012 NOTICE: hello, world
15:39:52 T:6820 NOTICE: hello, world
15:39:52 T:7080 NOTICE: hello, world
15:39:52 T:2556 NOTICE: hello, world
15:39:52 T:3904 NOTICE: hello, world
15:39:52 T:4284 NOTICE: hello, world
15:39:52 T:5648 NOTICE: hello, world
15:39:52 T:6076 NOTICE: hello, world
15:39:52 T:6516 NOTICE: hello, world
15:39:52 T:6552 NOTICE: hello, world
15:39:52 T:6768 NOTICE: hello, world
15:39:52 T:3464 NOTICE: hello, world
15:39:52 T:1240 NOTICE: hello, world
15:39:52 T:1992 NOTICE: hello, world
15:39:52 T:6896 NOTICE: hello, world
15:39:52 T:6288 NOTICE: hello, world
15:39:52 T:6212 NOTICE: hello, world
15:39:52 T:6672 NOTICE: hello, world
15:39:52 T:7040 NOTICE: hello, world
15:39:52 T:6280 NOTICE: hello, world
15:39:52 T:7112 NOTICE: hello, world
15:39:52 T:5760 NOTICE: hello, world
15:39:52 T:6664 NOTICE: hello, world
15:39:52 T:6952 NOTICE: hello, world
15:39:52 T:7024 NOTICE: hello, world
15:39:52 T:6224 NOTICE: hello, world
15:39:52 T:5356 NOTICE: hello, world
15:39:52 T:5628 NOTICE: hello, world
15:39:52 T:4220 NOTICE: hello, world
15:39:52 T:6160 NOTICE: hello, world
15:39:52 T:6320 NOTICE: hello, world
15:39:52 T:4384 NOTICE: hello, world
15:39:52 T:6572 NOTICE: hello, world
15:39:52 T:5284 NOTICE: hello, world
15:39:52 T:7060 NOTICE: hello, world
15:39:52 T:3256 NOTICE: hello, world
15:39:52 T:5008 NOTICE: hello, world
15:39:52 T:6124 NOTICE: hello, world
15:39:52 T:912 NOTICE: hello, world
15:39:52 T:5408 NOTICE: hello, world
15:39:52 T:5560 NOTICE: hello, world
15:39:52 T:4052 NOTICE: hello, world
15:39:52 T:4416 NOTICE: hello, world
15:39:52 T:5392 NOTICE: hello, world
15:39:52 T:6236 NOTICE: hello, world
15:39:52 T:5940 NOTICE: hello, world
15:39:52 T:540 NOTICE: hello, world
15:39:52 T:4596 NOTICE: hello, world
15:39:52 T:3796 NOTICE: hello, world
15:39:52 T:6700 NOTICE: hello, world
15:39:52 T:6772 NOTICE: hello, world
15:39:52 T:7092 NOTICE: hello, world
15:39:52 T:6868 NOTICE: hello, world
15:39:53 T:5292 NOTICE: hello, world
15:39:53 T:6440 NOTICE: hello, world
15:39:53 T:6060 NOTICE: hello, world
15:39:53 T:5360 NOTICE: hello, world
15:39:53 T:5984 NOTICE: hello, world
15:39:53 T:6900 NOTICE: hello, world
15:39:53 T:4412 NOTICE: hello, world
15:39:53 T:2812 NOTICE: hello, world
15:39:53 T:5756 NOTICE: hello, world
15:39:53 T:4048 NOTICE: hello, world
15:39:53 T:5596 NOTICE: hello, world
15:39:53 T:6936 NOTICE: hello, world
15:39:53 T:6612 NOTICE: hello, world
15:39:53 T:1768 NOTICE: hello, world
15:39:53 T:5496 NOTICE: hello, world
15:39:53 T:3168 NOTICE: hello, world
15:39:53 T:4444 NOTICE: hello, world
15:39:53 T:7036 NOTICE: hello, world
15:39:53 T:2896 NOTICE: hello, world
15:39:53 T:7164 NOTICE: hello, world
15:39:53 T:6032 NOTICE: hello, world
15:39:53 T:6912 NOTICE: hello, world
15:39:53 T:4448 NOTICE: hello, world
15:39:53 T:3132 NOTICE: hello, world
15:39:53 T:6708 NOTICE: hello, world
15:39:53 T:6556 NOTICE: hello, world
15:39:53 T:6184 NOTICE: hello, world
15:39:53 T:6588 NOTICE: hello, world
15:39:53 T:5700 NOTICE: hello, world
15:39:53 T:6404 NOTICE: hello, world
15:39:53 T:6244 NOTICE: hello, world
15:39:53 T:3528 NOTICE: hello, world
15:39:53 T:7132 NOTICE: hello, world
15:39:53 T:6808 NOTICE: hello, world
15:39:53 T:6004 NOTICE: hello, world
15:39:53 T:876 NOTICE: hello, world
15:39:53 T:5080 NOTICE: hello, world
15:39:53 T:6668 NOTICE: hello, world
15:39:53 T:5580 NOTICE: hello, world
15:39:53 T:5488 NOTICE: hello, world
15:39:53 T:6344 NOTICE: hello, world
15:39:53 T:5732 NOTICE: hello, world
15:39:53 T:4160 NOTICE: hello, world
15:39:53 T:6300 NOTICE: hello, world
15:39:53 T:3472 NOTICE: hello, world
15:39:53 T:2772 NOTICE: hello, world
15:39:53 T:3444 NOTICE: hello, world
15:39:53 T:6568 NOTICE: hello, world
15:39:53 T:6856 NOTICE: hello, world
15:39:53 T:984 NOTICE: hello, world
15:39:53 T:6748 NOTICE: hello, world
15:39:53 T:6920 NOTICE: hello, world
15:39:53 T:5712 NOTICE: hello, world
15:39:53 T:6248 NOTICE: hello, world
15:39:53 T:5532 NOTICE: hello, world
15:39:53 T:5344 NOTICE: hello, world
15:39:53 T:1484 NOTICE: hello, world
15:39:53 T:5804 NOTICE: hello, world
15:39:53 T:3632 NOTICE: hello, world
15:39:53 T:6780 NOTICE: hello, world
15:39:53 T:4760 NOTICE: hello, world
15:39:53 T:6888 NOTICE: hello, world
15:39:53 T:7156 NOTICE: hello, world
15:39:54 T:6932 NOTICE: hello, world
15:39:54 T:7144 NOTICE: hello, world
15:39:54 T:6980 NOTICE: hello, world
15:39:54 T:4128 NOTICE: hello, world
15:39:54 T:6956 NOTICE: hello, world
15:39:54 T:6924 NOTICE: hello, world
15:39:54 T:6376 NOTICE: hello, world
15:39:54 T:5820 NOTICE: hello, world
15:39:54 T:6052 NOTICE: hello, world
15:39:54 T:1696 NOTICE: hello, world
15:39:54 T:1312 NOTICE: hello, world
15:39:54 T:3804 NOTICE: hello, world
15:39:54 T:4208 NOTICE: hello, world
15:39:54 T:5428 NOTICE: hello, world
15:39:54 T:6408 NOTICE: hello, world
15:39:54 T:6728 NOTICE: hello, world
15:39:54 T:5544 NOTICE: hello, world
15:39:54 T:6448 NOTICE: hello, world
15:39:54 T:1132 NOTICE: hello, world
15:39:54 T:1892 NOTICE: hello, world
15:39:54 T:3856 NOTICE: hello, world
15:39:54 T:6416 NOTICE: hello, world
15:39:54 T:5256 NOTICE: hello, world
15:39:54 T:5536 NOTICE: hello, world
15:39:54 T:2860 NOTICE: hello, world
15:39:54 T:4844 NOTICE: hello, world
15:39:54 T:4268 NOTICE: hello, world
15:39:54 T:3636 NOTICE: hello, world
15:39:54 T:7052 NOTICE: hello, world
15:39:54 T:6264 NOTICE: hello, world
15:39:54 T:6508 NOTICE: hello, world
15:39:54 T:2728 NOTICE: hello, world
15:39:54 T:3608 NOTICE: hello, world
15:39:54 T:5764 NOTICE: hello, world
15:39:54 T:7064 NOTICE: hello, world
15:39:54 T:7148 NOTICE: hello, world
15:39:54 T:3616 NOTICE: hello, world
15:39:54 T:5716 NOTICE: hello, world
15:39:54 T:416 NOTICE: hello, world
15:39:54 T:6216 NOTICE: hello, world
15:39:54 T:1828 NOTICE: hello, world
15:39:54 T:5492 NOTICE: hello, world
15:39:54 T:6560 NOTICE: hello, world
15:39:54 T:4392 NOTICE: hello, world
15:39:54 T:7096 NOTICE: hello, world
15:39:54 T:6436 NOTICE: hello, world
15:39:54 T:5048 NOTICE: hello, world
15:39:54 T:6788 NOTICE: hello, world
15:39:54 T:3300 NOTICE: hello, world
15:39:54 T:6392 NOTICE: hello, world
15:39:54 T:5608 NOTICE: hello, world
15:39:54 T:6240 NOTICE: hello, world
15:39:54 T:4976 NOTICE: hello, world
15:39:54 T:5752 NOTICE: hello, world
15:39:54 T:2944 NOTICE: hello, world
15:39:54 T:4640 NOTICE: hello, world
15:39:54 T:2268 NOTICE: hello, world
15:39:54 T:6716 NOTICE: hello, world
15:39:54 T:2148 NOTICE: hello, world
15:39:54 T:5500 NOTICE: hello, world
15:39:54 T:5416 NOTICE: hello, world
15:39:54 T:5384 NOTICE: hello, world
15:39:54 T:5512 NOTICE: hello, world
15:39:54 T:5844 NOTICE: hello, world
15:39:54 T:2280 NOTICE: hello, world
15:39:54 T:2612 NOTICE: hello, world
15:39:54 T:6012 NOTICE: hello, world
15:39:54 T:6820 NOTICE: hello, world
15:39:54 T:3904 NOTICE: hello, world
15:39:54 T:5648 NOTICE: hello, world
15:39:54 T:6516 NOTICE: hello, world
15:39:54 T:6768 NOTICE: hello, world
15:39:54 T:3464 NOTICE: hello, world
15:39:54 T:6896 NOTICE: hello, world
15:39:54 T:7040 NOTICE: hello, world
15:39:54 T:6672 NOTICE: hello, world
15:39:54 T:5760 NOTICE: hello, world
15:39:54 T:6664 NOTICE: hello, world
15:39:54 T:7024 NOTICE: hello, world
15:39:54 T:5956 NOTICE: hello, world
15:39:54 T:6632 NOTICE: hello, world
15:39:54 T:5452 NOTICE: hello, world
15:39:54 T:5356 NOTICE: hello, world
15:39:54 T:6704 NOTICE: hello, world
15:39:54 T:6352 NOTICE: hello, world
15:39:54 T:6660 NOTICE: hello, world
15:39:54 T:1436 NOTICE: hello, world
15:39:54 T:6252 NOTICE: hello, world
15:39:54 T:6348 NOTICE: hello, world
15:39:54 T:6832 NOTICE: hello, world
15:39:54 T:5628 NOTICE: hello, world
15:39:54 T:4220 NOTICE: hello, world
15:39:54 T:6160 NOTICE: hello, world
15:39:55 T:7004 NOTICE: hello, world
15:39:55 T:6740 NOTICE: hello, world
15:39:55 T:6572 NOTICE: hello, world
15:39:55 T:6592 NOTICE: hello, world
15:39:55 T:7116 NOTICE: hello, world
15:39:55 T:6504 NOTICE: hello, world
15:39:55 T:5008 NOTICE: hello, world
15:39:55 T:6636 NOTICE: hello, world
15:39:55 T:1172 NOTICE: hello, world
15:39:55 T:4156 NOTICE: hello, world
15:39:55 T:5560 NOTICE: hello, world
15:39:55 T:6188 NOTICE: hello, world
15:39:55 T:6120 NOTICE: hello, world
15:39:55 T:6576 NOTICE: hello, world
15:39:55 T:6324 NOTICE: hello, world
15:39:55 T:6712 NOTICE: hello, world
15:39:55 T:5940 NOTICE: hello, world
15:39:55 T:6064 NOTICE: hello, world
15:39:55 T:7008 NOTICE: hello, world
15:39:55 T:4596 NOTICE: hello, world
15:39:55 T:6700 NOTICE: hello, world
15:39:55 T:7092 NOTICE: hello, world
15:39:55 T:5292 NOTICE: hello, world
15:39:55 T:6060 NOTICE: hello, world
15:39:55 T:5360 NOTICE: hello, world
15:39:55 T:4412 NOTICE: hello, world
15:39:55 T:2812 NOTICE: hello, world
15:39:55 T:6724 NOTICE: hello, world
15:39:55 T:4048 NOTICE: hello, world
15:39:55 T:2104 NOTICE: hello, world
15:39:55 T:5596 NOTICE: hello, world
15:39:55 T:5196 NOTICE: hello, world
15:39:55 T:2892 NOTICE: hello, world
15:39:55 T:4484 NOTICE: hello, world
15:39:55 T:6164 NOTICE: hello, world
15:39:55 T:6608 NOTICE: hello, world
15:39:55 T:6364 NOTICE: hello, world
15:39:55 T:4444 NOTICE: hello, world
15:39:55 T:7036 NOTICE: hello, world
15:39:55 T:2896 NOTICE: hello, world
15:39:55 T:6620 NOTICE: hello, world
15:39:55 T:6548 NOTICE: hello, world
15:39:55 T:788 NOTICE: hello, world
15:39:55 T:6696 NOTICE: hello, world
15:39:55 T:6656 NOTICE: hello, world
15:39:55 T:4672 NOTICE: hello, world
15:39:55 T:5224 NOTICE: hello, world
15:39:55 T:1500 NOTICE: hello, world
15:39:55 T:6332 NOTICE: hello, world
15:39:55 T:5260 NOTICE: hello, world
15:39:55 T:4924 NOTICE: hello, world
15:39:55 T:6964 NOTICE: hello, world
15:39:55 T:6960 NOTICE: hello, world
15:39:55 T:6880 NOTICE: hello, world
15:39:55 T:6304 NOTICE: hello, world
15:39:55 T:6168 NOTICE: hello, world
15:39:55 T:6928 NOTICE: hello, world
15:39:55 T:6600 NOTICE: hello, world
15:39:55 T:5240 NOTICE: hello, world
15:39:55 T:7164 NOTICE: hello, world
15:39:55 T:6872 NOTICE: hello, world
15:39:55 T:6824 NOTICE: hello, world
15:39:55 T:4616 NOTICE: hello, world
15:39:55 T:6708 NOTICE: hello, world
15:39:55 T:6556 NOTICE: hello, world
15:39:55 T:3224 NOTICE: hello, world
15:39:55 T:1032 NOTICE: hello, world
15:39:55 T:1492 NOTICE: hello, world
15:39:55 T:6008 NOTICE: hello, world
15:39:55 T:6904 NOTICE: hello, world
15:39:55 T:3440 NOTICE: hello, world
15:39:55 T:3528 NOTICE: hello, world
15:39:55 T:6004 NOTICE: hello, world
15:39:55 T:6232 NOTICE: hello, world
15:39:55 T:6808 NOTICE: hello, world
15:39:55 T:6668 NOTICE: hello, world
15:39:55 T:5732 NOTICE: hello, world
15:39:55 T:4160 NOTICE: hello, world
15:39:55 T:6300 NOTICE: hello, world
15:39:55 T:2772 NOTICE: hello, world
15:39:55 T:3444 NOTICE: hello, world
15:39:55 T:6568 NOTICE: hello, world
15:39:55 T:1240 NOTICE: hello, world
15:39:55 T:1992 NOTICE: hello, world
15:39:55 T:6212 NOTICE: hello, world
15:39:55 T:6920 NOTICE: hello, world
15:39:55 T:6248 NOTICE: hello, world
15:39:55 T:6280 NOTICE: hello, world
15:39:55 T:7112 NOTICE: hello, world
15:39:55 T:6952 NOTICE: hello, world
15:39:55 T:6224 NOTICE: hello, world
15:39:55 T:4172 NOTICE: hello, world
15:39:56 T:6780 NOTICE: hello, world
15:39:56 T:4760 NOTICE: hello, world
15:39:56 T:6888 NOTICE: hello, world
15:39:56 T:6308 NOTICE: hello, world
15:39:56 T:6752 NOTICE: hello, world
15:39:56 T:6764 NOTICE: hello, world
15:39:56 T:1856 NOTICE: hello, world
15:39:56 T:5464 NOTICE: hello, world
15:39:56 T:6784 NOTICE: hello, world
15:39:56 T:4384 NOTICE: hello, world
15:39:56 T:5232 NOTICE: hello, world
15:39:56 T:4128 NOTICE: hello, world
15:39:56 T:5284 NOTICE: hello, world
15:39:56 T:6376 NOTICE: hello, world
15:39:56 T:3256 NOTICE: hello, world
15:39:56 T:1312 NOTICE: hello, world
15:39:56 T:3804 NOTICE: hello, world
15:39:56 T:6408 NOTICE: hello, world
15:39:56 T:5392 NOTICE: hello, world
15:39:56 T:1132 NOTICE: hello, world
答案 0 :(得分:1)
我不熟悉你选择进行任务的方式,基本上睡觉应用程序听起来不好主意,你可能想找到一个更好的方法。
但是,如果您想要运行2个不同的操作,则需要使用threading。一个小例子
def function_1():
for i in range(5):
print "1111"
time.sleep(1)
def function_2():
for i in range(5):
print "2222"
time.sleep(1)
t1 = threading.Thread(target=function_1)
t2 = threading.Thread(target=function_2)
t1.start()
t2.start()
如果你被称为没有穿线的2个功能,那么你可以进出所有'111',然后是所有'222'。
使用线程时我得到了这个:
1111
2222
2222
1111
1111
2222
2222
1111
2222
1111
因为它发生了并行。 希望它足够清楚。
答案 1 :(得分:0)
time.sleep()
是一个阻塞函数,因此它会停止处理 - 您需要将数据库写入移动到单独的线程,请参阅线程或子进程的手册,或者需要从计时器更新字符串,也在手册中。