Skip to content

Commit 69ccb73

Browse files
committed
Move locking of fontlist.json *into* json_dump.
This makes it easier for end users to call json_dump (which is public API) safely, given that _lock_path is not public API.
1 parent b01325c commit 69ccb73

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,6 @@ The parameter ``s`` to `.Axes.annotate` and `.pyplot.annotate` is renamed to
147147
The old parameter name remains supported, but
148148
support for it will be dropped in a future Matplotlib release.
149149

150+
`.font_manager.json_dump` now locks the font manager dump file
151+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152+
... to prevent multiple processes from writing to it at the same time.

lib/matplotlib/font_manager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,11 @@ def json_dump(data, filename):
912912
File paths that are children of the Matplotlib data path (typically, fonts
913913
shipped with Matplotlib) are stored relative to that data path (to remain
914914
valid across virtualenvs).
915+
916+
This function temporarily locks the output file to prevent multiple
917+
processes from overwriting one another's output.
915918
"""
916-
with open(filename, 'w') as fh:
919+
with cbook._lock_path(filename), open(filename, 'w') as fh:
917920
try:
918921
json.dump(data, fh, cls=_JSONEncoder, indent=2)
919922
except OSError as e:
@@ -1333,8 +1336,7 @@ def _rebuild():
13331336
global fontManager
13341337
_log.info("Generating new fontManager, this may take some time...")
13351338
fontManager = FontManager()
1336-
with cbook._lock_path(_fmcache):
1337-
json_dump(fontManager, _fmcache)
1339+
json_dump(fontManager, _fmcache)
13381340

13391341

13401342
try:

0 commit comments

Comments
 (0)