diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index dfd6e12..5b1de08 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -689,6 +689,7 @@ class _PersistentBrowser: _BROWSER_MANAGER: Optional[_PersistentBrowser] = None _MD2IMG_RUNTIME = None +_MD2IMG_RUNTIME_LOCK = threading.Lock() class _Md2ImgRuntime: @@ -754,8 +755,12 @@ class _Md2ImgRuntime: def _get_md2img_runtime() -> _Md2ImgRuntime: global _MD2IMG_RUNTIME + # 并发首次访问时要加锁,避免创建出多个 runtime 实例, + # 进而出现“专用运行时已启动”日志重复与多线程并存问题。 if _MD2IMG_RUNTIME is None: - _MD2IMG_RUNTIME = _Md2ImgRuntime() + with _MD2IMG_RUNTIME_LOCK: + if _MD2IMG_RUNTIME is None: + _MD2IMG_RUNTIME = _Md2ImgRuntime() return _MD2IMG_RUNTIME