From 2453d035a3c2b1dbaf885802dbc467af386d10e9 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 21 Aug 2025 12:38:56 +1000 Subject: [PATCH] util: console: global console lock Add a lock around the internal `print` call to ensure that multithreaded applications using the module have clean output. Signed-off-by: Jordan Yates --- src/infuse_iot/util/console.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/infuse_iot/util/console.py b/src/infuse_iot/util/console.py index aafe5c2..b17b038 100644 --- a/src/infuse_iot/util/console.py +++ b/src/infuse_iot/util/console.py @@ -2,6 +2,7 @@ import datetime +import threading import colorama @@ -10,6 +11,8 @@ except NotImplementedError: pass +_lock = threading.Lock() + class Console: """Common terminal logging functions""" @@ -56,7 +59,8 @@ def log_rx(data_type, length, prefix=""): def log(timestamp: datetime.datetime, colour, string: str): """Log colourised string to terminal""" ts = timestamp.strftime("%H:%M:%S.%f")[:-3] - print(f"[{ts}]{colour} {string}") + with _lock: + print(f"[{ts}]{colour} {string}") def choose_one(title: str, options: list[str]) -> tuple[int, str]: