diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index ba41d6aa..d734434f 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -118,6 +118,8 @@ def start_ws_lang_server(port, check_parent_process, handler_class) -> None: with ThreadPoolExecutor(max_workers=10) as tpool: + send_queue = asyncio.Queue() + async def pylsp_ws(websocket): log.debug("Creating LSP object") @@ -146,14 +148,16 @@ def send_message(message, websocket): """Handler to send responses of processed requests to respective web socket clients""" try: payload = json.dumps(message, ensure_ascii=False) - asyncio.run(websocket.send(payload)) + asyncio.run(send_queue.put((payload, websocket))) except Exception as e: log.exception("Failed to write message %s, %s", message, str(e)) async def run_server(): async with websockets.serve(pylsp_ws, port=port): - # runs forever - await asyncio.Future() + while 1: + # Wait until payload is available for sending + payload, websocket = await send_queue.get() + await websocket.send(payload) asyncio.run(run_server())