From d112ea4ce930026f3627fca1b2d49ab3f00a4ab4 Mon Sep 17 00:00:00 2001 From: Tomuxs Date: Mon, 4 Aug 2025 14:34:04 +0200 Subject: [PATCH] Added threadpool --- py34/list.py | 13 +++++-------- py34/thread.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 py34/thread.py diff --git a/py34/list.py b/py34/list.py index 8bc44b7..e8491c2 100644 --- a/py34/list.py +++ b/py34/list.py @@ -2,8 +2,8 @@ from .post import Post from .scraper import scraper, ScraperException from .url import parse_thumbnail_url from .dockid import is_nochick, is_toodeep +from .thread import Pool import urllib.parse -from threading import Thread class ListException(Exception): @@ -55,13 +55,10 @@ class List: # Download thumbnails if fetch_thumbnails: - threads = [Thread(target=Post.get_thumbnail, args=(post,)) for post in self.posts] - - for thread in threads: - thread.start() - - for thread in threads: - thread.join() + pool = Pool() + for post in self.posts: + pool.submit(Post.get_thumbnail_data, post) + pool.join() except ScraperException as ex: raise ex diff --git a/py34/thread.py b/py34/thread.py new file mode 100644 index 0000000..a15b37b --- /dev/null +++ b/py34/thread.py @@ -0,0 +1,37 @@ +from threading import Thread +from typing import Callable + + +class Pool: + def __init__(self, max_workers: int = 5): + self.max_workers = max_workers + self.jobs: list[Thread] = [] + self.workers: list[Thread] = [] + + def submit(self, func: Callable, *vargs, **kwargs): + def proc(self, func: Callable, *vargs, **kwargs): + func(*vargs, **kwargs) + self._pool_proc() + + self.jobs.append(Thread( + target = proc, + args = (self, func, *vargs, ), + kwargs = kwargs + )) + + self._pool_proc() + + def join(self): + while len(self.workers) != 0: + self.workers[-1].join() + self._pool_proc() + + def _pool_proc(self): + # Remove any dead workers + self.workers = list(filter(Thread.is_alive, self.workers)) + + # Process jobs if any + while len(self.workers) < self.max_workers and len(self.jobs) != 0: + job = self.jobs.pop() + job.start() + self.workers.append(job)