Python QtNetwork - Download files threaded -


so im using qnetworkaccessmanager (and of course networkreply , request) downloads. can working totally fine when not threaded @ - , blocks application, no matter method try use run in non-blocking manner, wont work.

it seems .get() networkaccessmanager gets called, either never connects, or never sends signals writing.

if wondering why parts of code set way are, allow easier changing around attempt bunch of different methods.

one thing read on try using qtcore.qtimer.singleshot(0, call) on signal emits send control main event loop - tried no avail though.

from pyqt4 import qtnetwork, qtcore, qtgui import sys import os   class downloader(qtnetwork.qnetworkaccessmanager):     def __init__(self, url, dest):         super(downloader, self).__init__(none)          self.get_path = url         self.url = qtcore.qurl(url)         self.download_buffer = qtcore.qbytearray()          self.dest = self.set_up_destination(dest)          # self.request = qtnetwork.qnetworkrequest(self.url)         # self.reply = self.get(self.request)         #         # self.reply.readyread.connect(self.read_data)          self.finished.connect(self.write_finished)         print 'downloader inited'      def startdownload(self):         self.request = qtnetwork.qnetworkrequest(self.url)         self.reply = self.get(self.request)          self.reply.readyread.connect(self.read_data)      def set_up_destination(self, dest):         if '.' in dest:             dir_dest = os.path.dirname(dest)             if not os.path.isdir(dir_dest):                 os.makedirs(dir_dest)             return dest         else:             if not os.path.isdir(dest):                 os.makedirs(dest)             base = os.path.basename(self.get_path)             return os.path.join(dest, base).replace('\\', '/')      def write_finished(self):         write_file = qtcore.qfile(self.dest)         if write_file.open(qtcore.qiodevice.writeonly):             write_file.write(self.download_buffer)             write_file.close()             print 'wrote file: {0}'.format(os.path.basename(self.dest))         else:             print 'error'      def read_data(self):         self.download_buffer += self.reply.readall()      def print_progress(self, gotten, total):         gotten = gotten/float(1000000)         total = total/float(1000000)          self.total = total         divisor = total/5.0         if gotten > (divisor*self.notify_count):             self.notify_count += 1             print 'downloaded {0}/{1} mb'.format(gotten, total)         else:             pass   class downloadform(qtgui.qdialog):     def __init__(self, from_dir, to_dir):         super(downloadform, self).__init__(none)          self.from_dir = from_dir         self.to_dir = to_dir          self.downloaders = []          self.run_count = 0         self.done_count = 0         self.display_label = qtgui.qlabel('downloading items...')          self.vlayout = qtgui.qvboxlayout()         self.vlayout.addwidget(self.display_label)         self.setlayout(self.vlayout)          self.start_downloads(self.from_dir, self.to_dir)      def start_downloads(self, from_dir, to_dir):         items = os.path.listdir(from_dir)         items = [os.path.join(from_dir, x) x in items]           self.run_count = len(items)         self.display_label.settext('downloading {0} items...'.format(self.run_count))          item in items:             print 'starting item: {0}'.format(os.path.basename(from_dir))             m = downloader(item, to_dir)             m.finished.connect(self.adjust_run_count)             self.downloaders.append(m)          item in self.downloaders:             item.startdownload()      @qtcore.pyqtslot(object)     def adjust_run_count(self, v):         self.done_count += 1         if self.done_count >= self.run_count:             self.display_label.settext('all items finished!')   class thread(qtcore.qthread):     def __init__(self, lst):         super(thread, self).__init__(none)          self.lst = lst      def run(self):         item in self.lst:             item.startdownload()   if __name__ == '__main__':     app = qtgui.qapplication([])     dir1 = none #give path folder containing files     dir2 = none #give path empty folder 'copy' to.     form = downloadform('dir1',                         'dir2')     form.show()     app.exec_() 

its set run locally work.

this latest attempt (that figured wouldnt work, saw post similar somewhere other day figured id exhaust resources before coming here - 1 bad!!!)

i have tried things such as: created thread (as child of gui) made each manager object, stored var on how many managers made (ex, 50) connected finish signal own slot increment 'finished_count' var until hit 50 (meainign done) emit own signal, or print 'done' or something.

making separate threads per job in smiliar signal/slot manner

you idea, there few other methods tried - things have in common: - involved qthreads - either a) dont work, or b (more likely) work, , im doing wrong.

im bit @ loss here. hope can help!


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -