File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -1321,12 +1321,20 @@ def is_opentype_cff_font(filename):
1321
1321
return False
1322
1322
1323
1323
1324
- _get_font = lru_cache (64 )(ft2font .FT2Font )
1325
1324
_fmcache = os .path .join (
1326
1325
mpl .get_cachedir (), 'fontlist-v{}.json' .format (FontManager .__version__ ))
1327
1326
fontManager = None
1328
1327
1329
1328
1329
+ _get_font = lru_cache (64 )(ft2font .FT2Font )
1330
+ # FT2Font objects cannot be used across fork()s because they reference the same
1331
+ # FT_Library object. While invalidating *all* existing FT2Fonts after a fork
1332
+ # would be too complicated to be worth it, the main way FT2Fonts get reused is
1333
+ # via the cache of _get_font, which we can empty upon forking (in Py3.7+).
1334
+ if hasattr (os , "register_at_fork" ):
1335
+ os .register_at_fork (after_in_child = _get_font .cache_clear )
1336
+
1337
+
1330
1338
def get_font (filename , hinting_factor = None ):
1331
1339
if hinting_factor is None :
1332
1340
hinting_factor = rcParams ['text.hinting_factor' ]
Original file line number Diff line number Diff line change 1
1
from io import BytesIO
2
+ import multiprocessing
2
3
import os
3
4
from pathlib import Path
4
5
import shutil
@@ -184,3 +185,17 @@ def test_user_fonts_win32():
184
185
# Now, the font should be available
185
186
fonts = findSystemFonts ()
186
187
assert any (font_test_file in font for font in fonts )
188
+
189
+
190
+ def _model_handler (_ ):
191
+ fig , ax = plt .subplots ()
192
+ fig .savefig (BytesIO (), format = "pdf" )
193
+ plt .close ()
194
+
195
+
196
+ @pytest .mark .skipif (not hasattr (os , "register_at_fork" ),
197
+ reason = "Cannot register at_fork handlers" )
198
+ def test_fork ():
199
+ _model_handler (0 ) # Make sure the font cache is filled.
200
+ ctx = multiprocessing .get_context ("fork" )
201
+ ctx .Pool (processes = 2 ).map (_model_handler , range (2 ))
You can’t perform that action at this time.
0 commit comments