downloading the file and cancel in python -
i'm working on python script download xml file server , write data in sqlite3 database in parallel.
i need regarding how cancel connection server using urllib2 library. want database stop writing data away connection ahs been cancelled. allchannels_timer
called when user hits 'enter' button, wait 1 second before connecting server download xml file. want download stop when hit backspace button currently, code continues if nothing happened.
here current code:
import urllib2 import stringio import sqlite3 import threading sqlite3 import dbapi2 database xml.etree import elementtree import xml.etree.elementtree et userdict import dictmixin #get actioncodes keyboard.xml action_enter = 7 action_backspace = 110 def csetvisible(window,id,v=true): window.getcontrol(id).setvisible(v) class myclass(xbmcgui.windowxml): def timer1_8percent(self): in range(1): time.sleep(1) self.getcontrol(4202).setlabel("8%") def timer1_12percent(self): in range(1): time.sleep(2) self.getcontrol(4202).setlabel("12%") def timer1_18percent(self): in range(1): time.sleep(3) self.getcontrol(4202).setlabel("18%") def allchannels_timer(self): in range(1): time.sleep(0.3) self.getcontrol(4202).setlabel("0%") #download 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%") self.thread = threading.thread(target=self.timer1_8percent) self.thread.setdaemon(true) self.thread.start() self.thread = threading.thread(target=self.timer1_12percent) self.thread.setdaemon(true) self.thread.start() self.thread = threading.thread(target=self.timer1_18percent) self.thread.setdaemon(true) self.thread.start() 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() # loaded data channel in tv_elem.findall('channel'): channel_name = channel.find('display-name').text program in channel.findall('programme'): title = program.find('title').text start_time = program.get("start") stop_time = program.get("stop") cur.execute("insert programs(channel, title, start_date, stop_date)" + " values(?, ?, ?, ?)", [channel_name, title, start_time, stop_time]) con.commit() con.close print 'channels store database successfully!' program = none = datetime.datetime.now() #strch = '(\'' + '\',\''.join(channelmap.keys()) + '\')' cur.execute('select channel, title, start_date, stop_date programs channel') getprogram_info = cur.fetchall() row in getprogram_info: programming = row[0], row[1], row[2], row[3] print programming #print row[0], row[1], row[2], row[3] #programming = row[0], row[1], row[2], row[3] #programming = row[0], row[1], row[2], row[3] #cur.close() def onaction(self, action): img1_yellow = xbmc.getcondvisibility('control.isvisible(3)') if action == action_backspace: if img1_yellow: csetvisible(self,3,true) self.getcontrol(4202).setlabel("") #cancel connection , close database if action == action_enter: if img1_yellow: csetvisible(self,3,false) self.thread = threading.thread(target=self.allchannels_timer) self.thread.setdaemon(true) self.thread.start()
can please tell me how can cancel connection server xml file stop being downloaded. further know how can prevent data being written in database once connection has been cancelled.
i grateful regarding problem, example code highly appreciated.
to download file in background, while processing events in foreground, it's simpler use process
. have better python support, can cpu in addition i/o, , killable if act up.
the following starts background process. parent's main loop own thing, briefly checking if download done every , then. if download done, continues processing, exits.
have fun!
source
import logging, multiprocessing, time, urllib2 def download(url): mylog = multiprocessing.get_logger() mylog.info('downloading %s', url) time.sleep(2) req = urllib2.request(url) response = urllib2.urlopen(req) data = response.read() response.close() # more here time.sleep(3) mylog.info('done') def main(): mylog = multiprocessing.log_to_stderr(level=logging.info) mylog.info('start') download_proc = multiprocessing.process( target=download, args=('http://example.com',), ) download_proc.start() while true: mylog.info('ding') if download_proc.join(timeout=0.1) none: mylog.info('download done!') break time.sleep(1) mylog.info('done!') if __name__=='__main__': main()
output
[info/mainprocess] start [info/mainprocess] ding [info/process-1] child process calling self.run() [info/process-1] downloading http://example.com [info/mainprocess] download done! [info/mainprocess] done! [info/mainprocess] process shutting down [info/mainprocess] calling join() process process-1 [info/process-1] done [info/process-1] process shutting down [info/process-1] process exiting exitcode 0
Comments
Post a Comment