diff --git a/lib/esbonio/esbonio/lsp/sphinx/__init__.py b/lib/esbonio/esbonio/lsp/sphinx/__init__.py index 565f2114a..12295c3e1 100644 --- a/lib/esbonio/esbonio/lsp/sphinx/__init__.py +++ b/lib/esbonio/esbonio/lsp/sphinx/__init__.py @@ -2,12 +2,14 @@ import logging import pathlib import platform +import sys import traceback import typing import warnings from functools import partial from multiprocessing import Process from multiprocessing import Queue +from threading import Thread from typing import IO from typing import Any from typing import Dict @@ -48,6 +50,12 @@ from .line_number_transform import LineNumberTransform +# Use a Thread as a preview server instead of Process for Python 3.12+ +# Usable since Thread and Process shares the same API +PreviewExecutor = Process +if sys.version_info.major >= 3 and sys.version_info.minor >= 12: + PreviewExecutor = Thread + __all__ = [ "InitializationOptions", "MissingConfigError", @@ -434,14 +442,14 @@ def preview(self, options: Dict[str, Any]) -> Dict[str, Any]: server = make_preview_server(self.app.outdir) # type: ignore[arg-type] self.preview_port = server.server_port - self.preview_process = Process(target=server.serve_forever, daemon=True) + self.preview_process = PreviewExecutor(target=server.serve_forever, daemon=True) self.preview_process.start() if not self.preview_process and not IS_LINUX: self.logger.debug("Starting preview server") q: Queue = Queue() - self.preview_process = Process( + self.preview_process = PreviewExecutor( target=start_preview_server, args=(q, self.app.outdir), daemon=True ) self.preview_process.start()