From db6bff4c0636e6ea0b4b1b297e7fbc59bda7ad35 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Sun, 29 Oct 2023 15:30:10 +0100 Subject: [PATCH] Improve data read (#362) * Improve data read * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- plugins/terminals/fps_terminals/win_server.py | 6 +++--- plugins/terminals/pyproject.toml | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/terminals/fps_terminals/win_server.py b/plugins/terminals/fps_terminals/win_server.py index f54f7f66..f1865391 100644 --- a/plugins/terminals/fps_terminals/win_server.py +++ b/plugins/terminals/fps_terminals/win_server.py @@ -1,6 +1,8 @@ import asyncio import os +from functools import partial +from anyio import to_thread from winpty import PTY # type: ignore from jupyverse_api.terminals import TerminalServer @@ -32,12 +34,10 @@ async def serve(self, websocket): async def send_data(self): while True: try: - data = self.process.read(blocking=False) + data = await to_thread.run_sync(partial(self.process.read, blocking=True)) except Exception: await self.websocket.send_json(["disconnect", 1]) return - if not data: - await asyncio.sleep(0.1) else: for websocket in self.websockets: await websocket.send_json(["stdout", data]) diff --git a/plugins/terminals/pyproject.toml b/plugins/terminals/pyproject.toml index 2273d1b8..58b3481a 100644 --- a/plugins/terminals/pyproject.toml +++ b/plugins/terminals/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ "websockets", "pywinpty;platform_system=='Windows'", "jupyverse-api >=0.1.2,<1", + "anyio >=4,<5", ] dynamic = ["version"] [[project.authors]]