Skip to content

Commit 2453d03

Browse files
committed
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 <jordan@embeint.com>
1 parent 8195eb9 commit 2453d03

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/infuse_iot/util/console.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import datetime
5+
import threading
56

67
import colorama
78

@@ -10,6 +11,8 @@
1011
except NotImplementedError:
1112
pass
1213

14+
_lock = threading.Lock()
15+
1316

1417
class Console:
1518
"""Common terminal logging functions"""
@@ -56,7 +59,8 @@ def log_rx(data_type, length, prefix=""):
5659
def log(timestamp: datetime.datetime, colour, string: str):
5760
"""Log colourised string to terminal"""
5861
ts = timestamp.strftime("%H:%M:%S.%f")[:-3]
59-
print(f"[{ts}]{colour} {string}")
62+
with _lock:
63+
print(f"[{ts}]{colour} {string}")
6064

6165

6266
def choose_one(title: str, options: list[str]) -> tuple[int, str]:

0 commit comments

Comments
 (0)