From 3ae8992fb61d24104faa502cff275ba6a771cdbe Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:59:17 +0100 Subject: [PATCH] Clients: fix high CPU usage when launched via MultiProcessing (#4209) * Core: make Utils.stream_input not consume all CPU for non-blocking streams * Clients: ignore MultiProcessing pipe as input console --- CommonClient.py | 5 +++++ Utils.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/CommonClient.py b/CommonClient.py index 77ed85b5c6..47100a7383 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -710,6 +710,11 @@ class CommonContext: def run_cli(self): if sys.stdin: + if sys.stdin.fileno() != 0: + from multiprocessing import parent_process + if parent_process(): + return # ignore MultiProcessing pipe + # steam overlay breaks when starting console_loop if 'gameoverlayrenderer' in os.environ.get('LD_PRELOAD', ''): logger.info("Skipping terminal input, due to conflicting Steam Overlay detected. Please use GUI only.") diff --git a/Utils.py b/Utils.py index 412011200f..2dfcd9d3e1 100644 --- a/Utils.py +++ b/Utils.py @@ -18,6 +18,7 @@ import warnings from argparse import Namespace from settings import Settings, get_settings +from time import sleep from typing import BinaryIO, Coroutine, Optional, Set, Dict, Any, Union from typing_extensions import TypeGuard from yaml import load, load_all, dump @@ -568,6 +569,8 @@ def stream_input(stream: typing.TextIO, queue: "asyncio.Queue[str]"): else: if text: queue.put_nowait(text) + else: + sleep(0.01) # non-blocking stream from threading import Thread thread = Thread(target=queuer, name=f"Stream handler for {stream.name}", daemon=True)