Skip to content

Commit 427c952

Browse files
GH-63: Add an option to force the number of workers for concurrent processing (GH-64)
1 parent e555688 commit 427c952

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/thumbnails/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .thumbnail import ThumbnailVTT
2323
from .thumbnail import register_thumbnail
2424

25-
__version__ = "0.1.8"
25+
__version__ = "0.1.9"
2626
__all__ = (
2727
"Generator",
2828
"Thumbnail",

src/thumbnails/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .constants import DEFAULT_INTERVAL
1111
from .constants import DEFAULT_OUTPUT
1212
from .constants import DEFAULT_SKIP
13+
from .constants import DEFAULT_WORKERS
1314

1415
# Help messages of the particular option of the CLI.
1516
HELP_BASE = "The prefix of the thumbnails path can be customized."
@@ -18,6 +19,7 @@
1819
HELP_FORMAT = "Output format. Default is %s." % DEFAULT_FORMAT
1920
HELP_COMPRESS = "The image scale coefficient. A number from 0 to 1."
2021
HELP_INTERVAL = "The interval between neighbor thumbnails in seconds."
22+
HELP_WORKERS = "Workers number for concurrent processing. Default is calculated automatically."
2123

2224
# This defines a choice of supported values for the '--format' option of the CLI.
2325
format_choice = click.Choice(ThumbnailFactory.thumbnails.keys(), case_sensitive=False)
@@ -27,6 +29,7 @@ def cli(func):
2729
@click.command()
2830
@click.option("--compress", "-C", default=DEFAULT_COMPRESS, help=HELP_COMPRESS)
2931
@click.option("--interval", "-I", default=DEFAULT_INTERVAL, help=HELP_INTERVAL)
32+
@click.option("--workers", "-W", default=DEFAULT_WORKERS, help=HELP_WORKERS)
3033
@click.option("--base", "-B", default=DEFAULT_BASE, help=HELP_BASE)
3134
@click.option("--skip", "-S", default=DEFAULT_SKIP, help=HELP_SKIP, is_flag=True)
3235
@click.option("--output", "-O", default=DEFAULT_OUTPUT, type=click.Path(), help=HELP_OUTPUT)

src/thumbnails/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
DEFAULT_FORMAT = "vtt"
55
DEFAULT_COMPRESS = 1.0
66
DEFAULT_INTERVAL = 1.0
7+
DEFAULT_WORKERS = 0 # 0 is a flag for calculating the number of workers automatically.

src/thumbnails/generator.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .constants import DEFAULT_INTERVAL
1111
from .constants import DEFAULT_OUTPUT
1212
from .constants import DEFAULT_SKIP
13+
from .constants import DEFAULT_WORKERS
1314
from .pathtools import listdir
1415
from .progress import use_progress
1516
from .thumbnail import ThumbnailExistsError
@@ -34,10 +35,24 @@ def __init__(self, inputs):
3435
self.format = DEFAULT_FORMAT
3536
self.compress = DEFAULT_COMPRESS
3637
self.interval = DEFAULT_INTERVAL
38+
self._workers = DEFAULT_WORKERS
3739

3840
# Remove non-video files in case of input directory already contains other generated files.
3941
self.inputs = [file for file in self.inputs if re.match(r"^.*\.(?:(?!png|vtt|json).)+$", file)]
4042

43+
@property
44+
def workers(self):
45+
"""Returns the number of workers for concurrent processing."""
46+
if self._workers > 0:
47+
return self._workers
48+
# Limit the auto-calculated workers for super-multicore machines.
49+
return min(32, len(self.inputs), (os.cpu_count() or 1) + 4)
50+
51+
@workers.setter
52+
def workers(self, value):
53+
"""Sets the number of workers for concurrent processing."""
54+
self._workers = value
55+
4156
@staticmethod
4257
def worker(video, fmt, base, skip, output):
4358
"""Executes the required workflows for generating a thumbnail."""
@@ -60,7 +75,7 @@ def __next__(self):
6075

6176
@use_progress
6277
def generate(self):
63-
with concurrent.futures.ThreadPoolExecutor() as executor:
78+
with concurrent.futures.ThreadPoolExecutor(max_workers=self.workers) as executor:
6479
executor.map(
6580
functools.partial(
6681
self.worker,

0 commit comments

Comments
 (0)