Skip to content

Commit 0bac7bc

Browse files
authored
Simplified threading lock uses. (#500) (#501)
1 parent 8ba601c commit 0bac7bc

5 files changed

Lines changed: 5 additions & 20 deletions

File tree

leads/comm/prototype.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ def _register_process(self, *args, **kwargs) -> None:
5858
:param kwargs: kwargs passed to `run()`
5959
:exception RuntimeError: duplicated registration
6060
"""
61-
self._lock.acquire()
6261
if self._main_thread:
6362
raise RuntimeError("A service can only run once")
64-
try:
63+
with self._lock:
6564
self._main_thread = _Thread(name=f"service{hash(self)}", target=self._run, daemon=True, args=args,
6665
kwargs=kwargs)
67-
finally:
68-
self._lock.release()
6966

7067
def _parallel_run(self, *args, **kwargs) -> None:
7168
"""

leads/dt/odometer.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,5 @@ def __init__(self) -> None:
2525

2626
@_override
2727
def write(self, payload: float) -> None:
28-
self._lock.acquire()
29-
try:
28+
with self._lock:
3029
super().write(payload)
31-
finally:
32-
self._lock.release()

leads/logger.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ def format(self, msg: str, font: int, color: int | None, background: int | None)
5858
return f"\033[{font}{f";{color}" if color else ""}{f";{background + 10}" if background else ""}m{msg}\033[0m"
5959

6060
def print(self, msg: str, level: int) -> None:
61-
self._lock.acquire()
62-
try:
61+
with self._lock:
6362
if self._debug_level <= level:
6463
print(msg)
65-
finally:
66-
self._lock.release()
6764

6865
def info(self, *msg: str, sep: str = " ", end: str = "\n",
6966
f: tuple[int, int | None, int | None] = (REGULAR, None, None)) -> None:

leads/os.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ def active(self) -> bool:
2121

2222
@active.setter
2323
def active(self, active: bool) -> None:
24-
self._lock.acquire()
25-
try:
24+
with self._lock:
2625
self._active = active
27-
finally:
28-
self._lock.release()
2926

3027

3128
_thread_flags: _ThreadFlags = _ThreadFlags()

leads_comm_serial/identity.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ def _establish_connection_no_lock(self, serial: _Serial) -> SerialConnection:
5858
return self._establish_connection_no_lock(serial)
5959

6060
def establish_connection(self, serial: _Serial) -> SerialConnection:
61-
_lock.acquire()
62-
try:
61+
with _lock:
6362
return self._establish_connection_no_lock(serial)
64-
finally:
65-
_lock.release()
6663

6764

6865
_instances: dict[AutoIdentity, str | None] = {}

0 commit comments

Comments
 (0)