Skip to content

Commit 6b49030

Browse files
committed
Send websocket payload using a queue
1 parent 04fa3e5 commit 6b49030

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pylsp/python_lsp.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def start_ws_lang_server(port, check_parent_process, handler_class) -> None:
118118

119119
with ThreadPoolExecutor(max_workers=10) as tpool:
120120

121+
send_queue = asyncio.Queue()
122+
121123
async def pylsp_ws(websocket):
122124
log.debug("Creating LSP object")
123125

@@ -146,14 +148,16 @@ def send_message(message, websocket):
146148
"""Handler to send responses of processed requests to respective web socket clients"""
147149
try:
148150
payload = json.dumps(message, ensure_ascii=False)
149-
asyncio.run(websocket.send(payload))
151+
asyncio.run(send_queue.put((payload, websocket)))
150152
except Exception as e:
151153
log.exception("Failed to write message %s, %s", message, str(e))
152154

153155
async def run_server():
154156
async with websockets.serve(pylsp_ws, port=port):
155-
# runs forever
156-
await asyncio.Future()
157+
while 1:
158+
# Wait until payload is available for sending
159+
payload, websocket = await send_queue.get()
160+
await websocket.send(payload)
157161

158162
asyncio.run(run_server())
159163

0 commit comments

Comments
 (0)