@@ -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
6061def 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):
200214def 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