Skip to content

Commit

Permalink
add config value to control font fetching max_workers
Browse files Browse the repository at this point in the history
resolves #351
  • Loading branch information
2bndy5 committed Jul 5, 2024
1 parent 99ba8ab commit e660436
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ Configuration Options
bundles to the output directory. Source maps are helpful when developing the
theme.

.. confval:: sphinx_immaterial_font_fetch_max_workers

The number of workers executed in parallel when fetching a cache of the specified
:themeconf:`font`. If not specified or set to :python:`0`, then the system's CPU
count is used by default.

.. _version_dropdown:

Version Dropdown
Expand Down
10 changes: 8 additions & 2 deletions sphinx_immaterial/google_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,24 @@ def _adjust_css_urls(css_content: bytes, renamed_fonts: Dict[str, str]) -> str:
)


_MAX_CONCURRENT_FETCHES = 128
_MAX_CONCURRENT_FETCHES_KEY = "sphinx_immaterial_font_fetch_max_workers"

_TTF_FONT_PATHS_KEY = "sphinx_immaterial_ttf_font_paths"


def add_google_fonts(app: sphinx.application.Sphinx, fonts: List[str]):
cache_dir = os.path.join(get_cache_dir(app), "google_fonts")
static_dir = os.path.join(app.outdir, "_static")
max_workers = cast(
int, app.config.config_values.get(_MAX_CONCURRENT_FETCHES_KEY, 0)
)
if not max_workers:
max_workers = os.cpu_count() or 1
# _static path
font_dir = os.path.join(static_dir, "fonts")
os.makedirs(font_dir, exist_ok=True)

with concurrent.futures.ThreadPoolExecutor(max_workers=33) as executor:
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:

def to_thread(fn, *args, **kwargs) -> asyncio.Future:
return asyncio.wrap_future(executor.submit(fn, *args, **kwargs))
Expand Down Expand Up @@ -200,6 +205,7 @@ def _builder_inited(app: sphinx.application.Sphinx):
def setup(app: sphinx.application.Sphinx):
app.setup_extension("sphinx_immaterial.external_resource_cache")
app.connect("builder-inited", _builder_inited)
app.add_config_value(_MAX_CONCURRENT_FETCHES_KEY, default=0, rebuild="", types=int)

return {
"parallel_read_safe": True,
Expand Down

0 comments on commit e660436

Please sign in to comment.