diff --git a/src/config.py b/src/config.py index c34b4ab..8f61119 100644 --- a/src/config.py +++ b/src/config.py @@ -1,4 +1,5 @@ CREATE_GENERAL_CATEGORY: bool = True -THUMB_DIMENSION: tuple[int, int] = (200, 200) \ No newline at end of file +THUMB_DIMENSION: tuple[int, int] = (200, 200) +MAX_THREADS: int = 50 \ No newline at end of file diff --git a/src/picture.py b/src/picture.py index c2b3915..0c7692a 100644 --- a/src/picture.py +++ b/src/picture.py @@ -1,7 +1,7 @@ from __future__ import annotations from PIL import Image -import os +import thread_manager from path import Path @@ -61,12 +61,16 @@ class Picture(): return self._profile_file def gen_thumb(self): - im = Image.open(self._large.get_absolute_path()) - im.thumbnail(config.THUMB_DIMENSION) - im.save(self._thumb.get_absolute_path(), "JPEG") + def _(_large: Path, _thumb: Path): + im = Image.open(_large.get_absolute_path()) + im.thumbnail(config.THUMB_DIMENSION) + im.save(_thumb.get_absolute_path(), "JPEG") + thread_manager.add_to_queu(_, (self._large, self._thumb)) def gen_small(self): - im = Image.open(self._large.get_absolute_path()).convert("RGB") - im.save(self._small.get_absolute_path(), quality=95, optimize=True) + def _(_large: Path, _small: Path): + im = Image.open(_large.get_absolute_path()).convert("RGB") + im.save(_small.get_absolute_path(), quality=95, optimize=True) + thread_manager.add_to_queu(_, (self._large, self._small)) \ No newline at end of file diff --git a/src/thread_manager.py b/src/thread_manager.py new file mode 100644 index 0000000..dcbc540 --- /dev/null +++ b/src/thread_manager.py @@ -0,0 +1,8 @@ +import concurrent.futures +import config + +_pool = concurrent.futures.ThreadPoolExecutor(max_workers=config.MAX_THREADS) + +def add_to_queu(func: callable, args: tuple): + global _pool + _pool.submit(func, *args) \ No newline at end of file