def start_worker(self, row): '''为task新建一个后台下载线程, 并开始下载.''' def on_worker_received(worker, fs_id, current_size): GLib.idle_add(do_worker_received, fs_id, current_size) def do_worker_received(fs_id, current_size): row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: print('on worker received, row is None:', row) return row[CURRSIZE_COL] = current_size curr_size = util.get_human_size(row[CURRSIZE_COL])[0] total_size = util.get_human_size(row[SIZE_COL])[0] row[PERCENT_COL] = int(row[CURRSIZE_COL] / row[SIZE_COL] * 100) row[HUMANSIZE_COL] = '{0} / {1}'.format(curr_size, total_size) self.update_task_db(row) def on_worker_downloaded(worker, fs_id): GLib.idle_add(do_worker_downloaded, fs_id) def do_worker_downloaded(fs_id): row = self.get_row_by_fsid(fs_id) row = self.workers[fs_id][1] row[CURRSIZE_COL] = row[SIZE_COL] row[STATE_COL] = State.FINISHED row[PERCENT_COL] = 100 total_size = util.get_human_size(row[SIZE_COL])[0] row[HUMANSIZE_COL] = '{0} / {1}'.format(total_size, total_size) row[STATENAME_COL] = StateNames[State.FINISHED] self.update_task_db(row) self.workers.pop(row[FSID_COL], None) self.app.toast(_('{0} downloaded'.format(row[NAME_COL]))) self.launch_app(fs_id) self.scan_tasks() def on_worker_network_error(worker, fs_id): GLib.idle_add(do_worker_network_error, fs_id) def do_worker_network_error(fs_id): row = self.get_row_by_fsid(fs_id) row = self.workers[fs_id][1] row[STATE_COL] = State.ERROR row[STATENAME_COL] = StateNames[State.ERROR] self.update_task_db(row) self.remove_worker(row[FSID_COL]) if row[FSID_COL] in self.workers: return row[STATE_COL] = State.DOWNLOADING row[STATENAME_COL] = StateNames[State.DOWNLOADING] worker = Downloader(self, row, self.app.cookie, self.app.tokens) self.workers[row[FSID_COL]] = (worker, row) worker.connect('received', on_worker_received) worker.connect('downloaded', on_worker_downloaded) worker.connect('network-error', on_worker_network_error) worker.start()
def start_worker(self, row): '''为task新建一个后台下载线程, 并开始下载.''' print('start_worker() --', row[:]) def on_worker_received(worker, fs_id, current_size): row = self.get_task_by_fsid(fs_id) _, row = self.workers[fs_id] row[CURRSIZE_COL] = current_size total_size, _ = util.get_human_size(row[SIZE_COL]) curr_size, _ = util.get_human_size(row[CURRSIZE_COL]) row[PERCENT_COL] = int(row[CURRSIZE_COL] / row[SIZE_COL] * 100) row[HUMANSIZE_COL] = '{0} / {1}'.format(curr_size, total_size) def on_worker_downloaded(worker, fs_id): row = self.get_task_by_fsid(fs_id) _, row = self.workers[fs_id] row[CURRSIZE_COL] = row[SIZE_COL] row[STATE_COL] = State.FINISHED row[PERCENT_COL] = 100 total_size, _ = util.get_human_size(row[SIZE_COL]) row[HUMANSIZE_COL] = '{0} / {1}'.format(total_size, total_size) row[STATENAME_COL] = StateNames[State.FINISHED] del self.workers[fs_id] self.launch_app(fs_id) def on_worker_network_error(worker, fs_id): row = self.get_task_by_fsid(fs_id) _, row = self.workers[fs_id] row[STATE_COL] = State.ERROR row[STATENAME_COL] = StateNames[State.ERROR] if row[FSID_COL] in self.workers: return row[STATE_COL] = State.DOWNLOADING row[STATENAME_COL] = StateNames[State.DOWNLOADING] tree_iter = row.iter worker = Downloader(self, row, self.app.cookie, self.app.tokens) self.workers[row[FSID_COL]] = (worker, row) # TODO: add to main thread worker.connect('received', on_worker_received) worker.connect('downloaded', on_worker_downloaded) worker.connect('network-error', on_worker_network_error) worker.start()
def start_worker(self, row): '''为task新建一个后台下载线程, 并开始下载.''' def on_worker_started(worker, fs_id): GLib.idle_add(do_worker_started) def do_worker_started(): self.downloading_size = 0 self.downloading_timestamp = time.time() def on_worker_received(worker, fs_id, current_size): GLib.idle_add(do_worker_received, fs_id, current_size) def do_worker_received(fs_id, current_size): row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: return # update downloading speed self.downloading_size += current_size - row[CURRSIZE_COL] speed = (self.downloading_size / (time.time() - self.downloading_timestamp) / 1000) self.speed_label.set_text(_('{0} kb/s').format(int(speed))) row[CURRSIZE_COL] = current_size curr_size = util.get_human_size(row[CURRSIZE_COL])[0] total_size = util.get_human_size(row[SIZE_COL])[0] row[PERCENT_COL] = int(row[CURRSIZE_COL] / row[SIZE_COL] * 100) row[HUMANSIZE_COL] = '{0} / {1}'.format(curr_size, total_size) self.update_task_db(row) def on_worker_downloaded(worker, fs_id): GLib.idle_add(do_worker_downloaded, fs_id) def do_worker_downloaded(fs_id): row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[CURRSIZE_COL] = row[SIZE_COL] row[STATE_COL] = State.FINISHED row[PERCENT_COL] = 100 total_size = util.get_human_size(row[SIZE_COL])[0] row[HUMANSIZE_COL] = '{0} / {1}'.format(total_size, total_size) row[STATENAME_COL] = StateNames[State.FINISHED] self.update_task_db(row) self.workers.pop(row[FSID_COL], None) self.app.toast(_('{0} downloaded'.format(row[NAME_COL]))) self.launch_app(fs_id) self.scan_tasks() def on_worker_network_error(worker, fs_id): GLib.idle_add(do_worker_network_error, fs_id) def do_worker_network_error(fs_id): row = self.workers.get(fs_id, None) if row: row = row[1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[STATE_COL] = State.ERROR row[STATENAME_COL] = StateNames[State.ERROR] self.update_task_db(row) self.remove_worker(row[FSID_COL]) self.app.toast(_('Error occurs will downloading {0}').format( row[NAME_COL])) self.scan_tasks() if row[FSID_COL] in self.workers: return row[STATE_COL] = State.DOWNLOADING row[STATENAME_COL] = StateNames[State.DOWNLOADING] worker = Downloader(self, row, self.app.cookie, self.app.tokens) self.workers[row[FSID_COL]] = (worker, row) worker.connect('started', on_worker_started) worker.connect('received', on_worker_received) worker.connect('downloaded', on_worker_downloaded) worker.connect('network-error', on_worker_network_error) worker.start()
def start_worker(self, row): '''为task新建一个后台下载线程, 并开始下载.''' def on_worker_started(worker, fs_id): pass def on_worker_received(worker, fs_id, received, received_total): GLib.idle_add(do_worker_received, fs_id, received, received_total) def do_worker_received(fs_id, received, received_total): self.download_speed_add(received) row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[CURRSIZE_COL] = received_total curr_size = util.get_human_size(row[CURRSIZE_COL], False)[0] total_size = util.get_human_size(row[SIZE_COL])[0] row[PERCENT_COL] = int(row[CURRSIZE_COL] / row[SIZE_COL] * 100) row[HUMANSIZE_COL] = '{0} / {1}'.format(curr_size, total_size) self.update_task_db(row) def on_worker_downloaded(worker, fs_id): GLib.idle_add(do_worker_downloaded, fs_id) def do_worker_downloaded(fs_id): row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[CURRSIZE_COL] = row[SIZE_COL] row[STATE_COL] = State.FINISHED row[PERCENT_COL] = 100 total_size = util.get_human_size(row[SIZE_COL])[0] row[HUMANSIZE_COL] = '{0} / {1}'.format(total_size, total_size) row[STATENAME_COL] = StateNames[State.FINISHED] self.update_task_db(row) self.check_commit(force=True) self.workers.pop(row[FSID_COL], None) self.app.toast(_('{0} downloaded'.format(row[NAME_COL]))) self.launch_app(fs_id) self.scan_tasks() def on_worker_network_error(worker, fs_id): GLib.idle_add(do_worker_network_error, fs_id) def do_worker_network_error(fs_id): row = self.workers.get(fs_id, None) if row: row = row[1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[STATE_COL] = State.ERROR row[STATENAME_COL] = StateNames[State.ERROR] self.update_task_db(row) self.remove_worker(row[FSID_COL], stop=False) if self.app.profile['retries-each']: GLib.timeout_add(self.app.profile['retries-each'] * 60000, self.restart_task, row) else: self.app.toast( _('Error occurs will downloading {0}').format( row[NAME_COL])) self.scan_tasks() def do_worker_disk_error(fs_id, tmp_filepath): # do not retry on disk-error self.app.toast( _('Disk Error: failed to read/write {0}').format(tmp_filepath)) def on_worker_disk_error(worker, fs_id, tmp_filepath): GLib.idle_add(do_worker_disk_error, fs_id, tmp_filepath) if not row or row[FSID_COL] in self.workers: return row[STATE_COL] = State.DOWNLOADING row[STATENAME_COL] = StateNames[State.DOWNLOADING] worker = Downloader(self, row) self.workers[row[FSID_COL]] = (worker, row) worker.connect('started', on_worker_started) worker.connect('received', on_worker_received) worker.connect('downloaded', on_worker_downloaded) worker.connect('network-error', on_worker_network_error) worker.connect('disk-error', on_worker_disk_error) worker.start()
def start_worker(self, row): '''为task新建一个后台下载线程, 并开始下载.''' def on_worker_started(worker, fs_id): pass def on_worker_received(worker, fs_id, received, received_total): GLib.idle_add(do_worker_received, fs_id, received, received_total) def do_worker_received(fs_id, received, received_total): self.download_speed_add(received) row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[CURRSIZE_COL] = received_total curr_size = util.get_human_size(row[CURRSIZE_COL], False)[0] total_size = util.get_human_size(row[SIZE_COL])[0] row[PERCENT_COL] = int(row[CURRSIZE_COL] / row[SIZE_COL] * 100) row[HUMANSIZE_COL] = '{0} / {1}'.format(curr_size, total_size) self.update_task_db(row) def on_worker_downloaded(worker, fs_id): GLib.idle_add(do_worker_downloaded, fs_id) def do_worker_downloaded(fs_id): row = None if fs_id in self.workers: row = self.workers[fs_id][1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[CURRSIZE_COL] = row[SIZE_COL] row[STATE_COL] = State.FINISHED row[PERCENT_COL] = 100 total_size = util.get_human_size(row[SIZE_COL])[0] row[HUMANSIZE_COL] = '{0} / {1}'.format(total_size, total_size) row[STATENAME_COL] = StateNames[State.FINISHED] self.update_task_db(row) self.check_commit(force=True) self.workers.pop(row[FSID_COL], None) self.app.toast(_('{0} downloaded'.format(row[NAME_COL]))) self.launch_app(fs_id) self.scan_tasks() def on_worker_network_error(worker, fs_id): GLib.idle_add(do_worker_network_error, fs_id) def do_worker_network_error(fs_id): row = self.workers.get(fs_id, None) if row: row = row[1] else: row = self.get_row_by_fsid(fs_id) if not row: return row[STATE_COL] = State.ERROR row[STATENAME_COL] = StateNames[State.ERROR] self.update_task_db(row) self.remove_worker(row[FSID_COL], stop=False) if self.app.profile['retries-each']: GLib.timeout_add(self.app.profile['retries-each'] * 60000, self.restart_task, row) else: self.app.toast(_('Error occurs will downloading {0}').format( row[NAME_COL])) self.scan_tasks() def do_worker_disk_error(fs_id, tmp_filepath): # do not retry on disk-error self.app.toast(_('Disk Error: failed to read/write {0}').format( tmp_filepath)) def on_worker_disk_error(worker, fs_id, tmp_filepath): GLib.idle_add(do_worker_disk_error, fs_id, tmp_filepath) if not row or row[FSID_COL] in self.workers: return row[STATE_COL] = State.DOWNLOADING row[STATENAME_COL] = StateNames[State.DOWNLOADING] worker = Downloader(self, row) self.workers[row[FSID_COL]] = (worker, row) worker.connect('started', on_worker_started) worker.connect('received', on_worker_received) worker.connect('downloaded', on_worker_downloaded) worker.connect('network-error', on_worker_network_error) worker.connect('disk-error', on_worker_disk_error) worker.start()