| 309 | |
| 310 | |
| 311 | def _test_threading(): |
| 312 | import threading |
| 313 | from matplotlib.ft2font import LoadFlags |
| 314 | import matplotlib.font_manager as fm |
| 315 | |
| 316 | def loud_excepthook(args): |
| 317 | raise RuntimeError("error in thread!") |
| 318 | |
| 319 | threading.excepthook = loud_excepthook |
| 320 | |
| 321 | N = 10 |
| 322 | b = threading.Barrier(N) |
| 323 | |
| 324 | def bad_idea(n): |
| 325 | b.wait(timeout=5) |
| 326 | for j in range(100): |
| 327 | font = fm.get_font(fm.findfont("DejaVu Sans")) |
| 328 | font.set_text(str(n), 0.0, flags=LoadFlags.NO_HINTING) |
| 329 | |
| 330 | threads = [ |
| 331 | threading.Thread(target=bad_idea, name=f"bad_thread_{j}", args=(j,)) |
| 332 | for j in range(N) |
| 333 | ] |
| 334 | |
| 335 | for t in threads: |
| 336 | t.start() |
| 337 | |
| 338 | for t in threads: |
| 339 | t.join(timeout=9) |
| 340 | if t.is_alive(): |
| 341 | raise RuntimeError("thread failed to join") |
| 342 | |
| 343 | |
| 344 | def test_fontcache_thread_safe(): |