def start_worker(self, row):
def on_worker_started(worker, fs_id):<EOL><INDENT>pass<EOL><DEDENT>def on_worker_received(worker, fs_id, received, received_total):<EOL><INDENT>GLib.idle_add(do_worker_received, fs_id, received, received_total)<EOL><DEDENT>def do_worker_received(fs_id, received, received_total):<EOL><INDENT>self.download_speed_add(received)<EOL>row = None<EOL>if fs_id in self.workers:<EOL><INDENT>row = self.workers[fs_id][<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>row = self.get_row_by_fsid(fs_id)<EOL><DEDENT>if not row:<EOL><INDENT>return<EOL><DEDENT>row[CURRSIZE_COL] = received_total<EOL>curr_size = util.get_human_size(row[CURRSIZE_COL], False)[<NUM_LIT:0>]<EOL>total_size = util.get_human_size(row[SIZE_COL])[<NUM_LIT:0>]<EOL>row[PERCENT_COL] = int(row[CURRSIZE_COL] / row[SIZE_COL] * <NUM_LIT:100>)<EOL>row[HUMANSIZE_COL] = '<STR_LIT>'.format(curr_size, total_size)<EOL>self.update_task_db(row)<EOL><DEDENT>def on_worker_downloaded(worker, fs_id):<EOL><INDENT>GLib.idle_add(do_worker_downloaded, fs_id)<EOL><DEDENT>def do_worker_downloaded(fs_id):<EOL><INDENT>row = None<EOL>if fs_id in self.workers:<EOL><INDENT>row = self.workers[fs_id][<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>row = self.get_row_by_fsid(fs_id)<EOL><DEDENT>if not row:<EOL><INDENT>return<EOL><DEDENT>row[CURRSIZE_COL] = row[SIZE_COL]<EOL>row[STATE_COL] = State.FINISHED<EOL>row[PERCENT_COL] = <NUM_LIT:100><EOL>total_size = util.get_human_size(row[SIZE_COL])[<NUM_LIT:0>]<EOL>row[HUMANSIZE_COL] = '<STR_LIT>'.format(total_size, total_size)<EOL>row[STATENAME_COL] = StateNames[State.FINISHED]<EOL>self.update_task_db(row)<EOL>self.check_commit(force=True)<EOL>self.workers.pop(row[FSID_COL], None)<EOL>self.app.toast(_('<STR_LIT>'.format(row[NAME_COL])))<EOL>self.launch_app(fs_id)<EOL>self.scan_tasks()<EOL><DEDENT>def on_worker_network_error(worker, fs_id):<EOL><INDENT>GLib.idle_add(do_worker_network_error, fs_id)<EOL><DEDENT>def do_worker_network_error(fs_id):<EOL><INDENT>row = self.workers.get(fs_id, None)<EOL>if row:<EOL><INDENT>row = row[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>row = self.get_row_by_fsid(fs_id)<EOL>if not row:<EOL><INDENT>return<EOL><DEDENT><DEDENT>row[STATE_COL] = State.ERROR<EOL>row[STATENAME_COL] = StateNames[State.ERROR]<EOL>self.update_task_db(row)<EOL>self.remove_worker(row[FSID_COL], stop=False)<EOL>if self.app.profile['<STR_LIT>']:<EOL><INDENT>GLib.timeout_add(self.app.profile['<STR_LIT>'] * <NUM_LIT>,<EOL>self.restart_task, row)<EOL><DEDENT>else:<EOL><INDENT>self.app.toast(_('<STR_LIT>').format(<EOL>row[NAME_COL]))<EOL><DEDENT>self.scan_tasks()<EOL><DEDENT>def do_worker_disk_error(fs_id, tmp_filepath):<EOL><INDENT>self.app.toast(_('<STR_LIT>').format(<EOL>tmp_filepath))<EOL><DEDENT>def on_worker_disk_error(worker, fs_id, tmp_filepath):<EOL><INDENT>GLib.idle_add(do_worker_disk_error, fs_id, tmp_filepath)<EOL><DEDENT>if not row or row[FSID_COL] in self.workers:<EOL><INDENT>return<EOL><DEDENT>row[STATE_COL] = State.DOWNLOADING<EOL>row[STATENAME_COL] = StateNames[State.DOWNLOADING]<EOL>worker = Downloader(self, row)<EOL>self.workers[row[FSID_COL]] = (worker, row)<EOL>worker.connect('<STR_LIT>', on_worker_started)<EOL>worker.connect('<STR_LIT>', on_worker_received)<EOL>worker.connect('<STR_LIT>', on_worker_downloaded)<EOL>worker.connect('<STR_LIT>', on_worker_network_error)<EOL>worker.connect('<STR_LIT>', on_worker_disk_error)<EOL>worker.start()<EOL>
为task新建一个后台下载线程, 并开始下载.