From 8dc7a1f40bc577efa5fb657788e776b4050d0385 Mon Sep 17 00:00:00 2001 From: RicterZ Date: Thu, 1 Aug 2019 18:52:30 +0800 Subject: [PATCH] singleton pool --- nhentai/downloader.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/nhentai/downloader.py b/nhentai/downloader.py index eee7d78..3d2db81 100644 --- a/nhentai/downloader.py +++ b/nhentai/downloader.py @@ -27,6 +27,17 @@ class NHentaiImageNotExistException(Exception): pass +class Pool(Singleton): + pool = None + + def __init__(self, size, init): + if self.pool is None: + if os.getenv('DEBUG'): + logger.info('Process pool created') + + self.pool = mp.Pool(size, initializer=init) + + class Downloader(Singleton): def __init__(self, path='', thread=1, timeout=30, delay=0): @@ -135,7 +146,7 @@ class Downloader(Singleton): queue = [(self, url, folder) for url in queue] - pool = mp.Pool(self.thread_count, init_worker) + pool = Pool(self.thread_count, init_worker).pool for item in queue: pool.apply_async(download_wrapper, args=item, callback=self._download_callback)