Now incrementing _files_downloaded happens when _progress_callback is set. And made super() with no args.
This commit is contained in:
parent
e7cf2e7c4b
commit
2f9d26427c
@ -7,25 +7,26 @@ from multiprocessing.pool import ThreadPool
|
|||||||
from scrapthechan.scraper import Scraper
|
from scrapthechan.scraper import Scraper
|
||||||
from scrapthechan.fileinfo import FileInfo
|
from scrapthechan.fileinfo import FileInfo
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["ThreadedScraper"]
|
__all__ = ["ThreadedScraper"]
|
||||||
|
|
||||||
|
|
||||||
class ThreadedScraper(Scraper):
|
class ThreadedScraper(Scraper):
|
||||||
def __init__(self, save_directory: str, files: List[FileInfo],
|
def __init__(self, save_directory: str, files: List[FileInfo],
|
||||||
download_progress_callback: Callable[[int], None] = None) -> None:
|
download_progress_callback: Callable[[int], None] = None) -> None:
|
||||||
super(ThreadedScraper, self).__init__(save_directory, files,
|
super().__init__(save_directory, files, download_progress_callback)
|
||||||
download_progress_callback)
|
self._files_downloaded = 0
|
||||||
self._files_downloaded = 0
|
self._files_downloaded_mutex = Lock()
|
||||||
self._files_downloaded_mutex = Lock()
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
pool = ThreadPool(cpu_count() * 2)
|
pool = ThreadPool(cpu_count() * 2)
|
||||||
pool.map(self._thread_run, self._files)
|
pool.map(self._thread_run, self._files)
|
||||||
pool.close()
|
pool.close()
|
||||||
pool.join()
|
pool.join()
|
||||||
|
|
||||||
def _thread_run(self, f: FileInfo):
|
def _thread_run(self, f: FileInfo):
|
||||||
with self._files_downloaded_mutex:
|
if not self._progress_callback is None:
|
||||||
self._files_downloaded += 1
|
with self._files_downloaded_mutex:
|
||||||
if not self._progress_callback is None:
|
self._files_downloaded += 1
|
||||||
self._progress_callback(self._files_downloaded)
|
self._progress_callback(self._files_downloaded)
|
||||||
self._download_file(f)
|
self._download_file(f)
|
||||||
|
Loading…
Reference in New Issue
Block a user