From 96602d1890582f363e28a3a811a922a7d6341ae2 Mon Sep 17 00:00:00 2001 From: derrod Date: Sat, 25 Apr 2020 10:18:45 +0200 Subject: [PATCH] [downloader/models] Minor code cleanups --- legendary/downloader/manager.py | 21 +++++++++------------ legendary/models/downloading.py | 3 +-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/legendary/downloader/manager.py b/legendary/downloader/manager.py index a8e5f9f..e787409 100644 --- a/legendary/downloader/manager.py +++ b/legendary/downloader/manager.py @@ -86,7 +86,7 @@ class DLManager(Process): c_guid = self.chunks_to_dl.popleft() chunk = self.chunk_data_list.get_chunk_by_guid(c_guid) - self.log.debug(f'Adding {chunk.guid_str} (active: {self.active_tasks})') + self.log.debug(f'Adding {chunk.guid_num} (active: {self.active_tasks})') try: self.dl_worker_queue.put(DownloaderTask(url=self.base_url + '/' + chunk.path, chunk_guid=c_guid, shm=sms), @@ -147,11 +147,9 @@ class DLManager(Process): continue while (task.chunk_guid in in_buffer) or task.chunk_file: - if task.chunk_file: # re-using from an old file - res_shm = None - else: - res = in_buffer[task.chunk_guid] - res_shm = res.shm + res_shm = None + if not task.chunk_file: # not re-using from an old file + res_shm = in_buffer[task.chunk_guid].shm try: self.writer_queue.put(WriterTask( @@ -194,7 +192,7 @@ class DLManager(Process): self.active_tasks += 1 except Exception as e: self.log.warning(f'Failed adding retry task to queue! {e!r}') - # if no reserved memory, add to the beginning of the normal queue + # If this failed for whatever reason, put the chunk at the front of the DL list self.chunks_to_dl.appendleft(res.chunk_guid) except Empty: pass @@ -351,10 +349,10 @@ class DLManager(Process): for next_chunk in chunkstream_starts: self.log.debug(f'- Chunkstream start: {next_chunk!r}') - while file_list := chunk_to_file_map.get(next_chunk.guid_num): - current_file = file_list.popleft() + while file_deque := chunk_to_file_map.get(next_chunk.guid_num): + current_file = file_deque.popleft() - if len(file_list) == 0: + if len(file_deque) == 0: del chunk_to_file_map[next_chunk.guid_num] # skip unchanged files @@ -474,8 +472,7 @@ class DLManager(Process): # create the shared memory segments and add them to their respective pools for i in range(int(self.shared_memory.size / self.analysis.biggest_chunk)): _sms = SharedMemorySegment(offset=i * self.analysis.biggest_chunk, - end=i * self.analysis.biggest_chunk + self.analysis.biggest_chunk, - _id=i) + end=i * self.analysis.biggest_chunk + self.analysis.biggest_chunk) self.sms.append(_sms) self.log.debug(f'Created {len(self.sms)} shared memory segments.') diff --git a/legendary/models/downloading.py b/legendary/models/downloading.py index 88caf08..f5c0513 100644 --- a/legendary/models/downloading.py +++ b/legendary/models/downloading.py @@ -85,10 +85,9 @@ class SharedMemorySegment: Segment of the shared memory used for one Chunk """ - def __init__(self, offset=0, end=1024 * 1024, _id=None): + def __init__(self, offset=0, end=1024 * 1024): self.offset = offset self.end = end - self._id = _id @property def size(self):