From 4199705fe8cc852d45f20c81fd71e1be87119cc5 Mon Sep 17 00:00:00 2001 From: Randi Date: Thu, 13 Jun 2024 19:59:35 -0400 Subject: [PATCH] Expanded scope of thread lock in screen::grab() method. --- src/screen.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/screen.py b/src/screen.py index 78b788f..fe1f850 100644 --- a/src/screen.py +++ b/src/screen.py @@ -73,15 +73,14 @@ def grab(force_new: bool = False) -> np.ndarray: global cached_img global last_grab # with 25fps we have 40ms per frame. If we check for 20ms range to make sure we can still get each frame if we want. - if not force_new and cached_img is not None and last_grab is not None and time.perf_counter() - last_grab < 0.04: - return cached_img - else: - with cached_img_lock: + with cached_img_lock: + if not force_new and cached_img is not None and last_grab is not None and time.perf_counter() - last_grab < 0.04: + return cached_img + else: last_grab = time.perf_counter() - img = np.array(sct.grab(monitor_roi)) - with cached_img_lock: + img = np.array(sct.grab(monitor_roi)) cached_img = img[:, :, :3] - return cached_img + return cached_img # TODO: Move the below funcs to utils(?)