Skip to content

Commit a930823

Browse files
authored
add config value to control font fetching max_workers (#363)
resolves #351 use hard-coded default of 128 max_workers support env var as override
1 parent 3951651 commit a930823

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

docs/customization.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,19 @@ Configuration Options
742742
bundles to the output directory. Source maps are helpful when developing the
743743
theme.
744744

745+
.. confval:: sphinx_immaterial_font_fetch_max_workers
746+
747+
The number of workers executed in parallel when fetching a cache of the specified
748+
:themeconf:`font`. If not specified, this defaults to using 33 maximum *possible* threads.
749+
If set less than or equal to 0, then this will be determined by the
750+
:py:class:`~concurrent.futures.ThreadPoolExecutor` default.
751+
752+
.. envvar:: SPHINX_IMMATERIAL_FONT_FETCH_MAX_WORKERS
753+
754+
An environment variable that can be used to override
755+
:confval:`sphinx_immaterial_font_fetch_max_workers`.
756+
This value must be an integer.
757+
745758
.. _version_dropdown:
746759

747760
Version Dropdown

sphinx_immaterial/google_fonts.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,33 @@ def _adjust_css_urls(css_content: bytes, renamed_fonts: Dict[str, str]) -> str:
5252
)
5353

5454

55-
_MAX_CONCURRENT_FETCHES = 128
55+
_MAX_CONCURRENT_FETCHES_KEY = "sphinx_immaterial_font_fetch_max_workers"
56+
_MAX_CONCURRENT_FETCHES_ENV_KEY = "SPHINX_IMMATERIAL_FONT_FETCH_MAX_WORKERS"
5657

5758
_TTF_FONT_PATHS_KEY = "sphinx_immaterial_ttf_font_paths"
5859

5960

6061
def add_google_fonts(app: sphinx.application.Sphinx, fonts: List[str]):
6162
cache_dir = os.path.join(get_cache_dir(app), "google_fonts")
6263
static_dir = os.path.join(app.outdir, "_static")
64+
max_workers: Optional[int] = cast(
65+
int, app.config.config_values.get(_MAX_CONCURRENT_FETCHES_KEY, 128)
66+
)
67+
if _MAX_CONCURRENT_FETCHES_ENV_KEY in os.environ:
68+
try:
69+
max_workers = int(os.environ[_MAX_CONCURRENT_FETCHES_ENV_KEY])
70+
except ValueError:
71+
logger.warning(
72+
"Environment variable, %s, must be an integer value.",
73+
_MAX_CONCURRENT_FETCHES_ENV_KEY,
74+
)
75+
if max_workers is not None and max_workers <= 0:
76+
max_workers = None # use default from ThreadPoolExecutor
6377
# _static path
6478
font_dir = os.path.join(static_dir, "fonts")
6579
os.makedirs(font_dir, exist_ok=True)
6680

67-
with concurrent.futures.ThreadPoolExecutor(max_workers=33) as executor:
81+
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
6882

6983
def to_thread(fn, *args, **kwargs) -> asyncio.Future:
7084
return asyncio.wrap_future(executor.submit(fn, *args, **kwargs))
@@ -200,6 +214,9 @@ def _builder_inited(app: sphinx.application.Sphinx):
200214
def setup(app: sphinx.application.Sphinx):
201215
app.setup_extension("sphinx_immaterial.external_resource_cache")
202216
app.connect("builder-inited", _builder_inited)
217+
app.add_config_value(
218+
_MAX_CONCURRENT_FETCHES_KEY, default=128, rebuild="", types=int
219+
)
203220

204221
return {
205222
"parallel_read_safe": True,

0 commit comments

Comments
 (0)