From 82f84beb2ab04bd3ada528952959e36718e46c2c Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 7 Apr 2026 13:54:28 +0300 Subject: [PATCH] cli: reuse gesture instance; cancel pending tasks on shutdown Signed-off-by: NotAShelf Change-Id: Id1471dd8fb5a56d3cfa514d236624d686a6a6964 --- src/flipoff/cli.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/flipoff/cli.py b/src/flipoff/cli.py index a6be101..f089842 100644 --- a/src/flipoff/cli.py +++ b/src/flipoff/cli.py @@ -14,13 +14,13 @@ from flipoff.gesture import GestureRegistry def _get_callback( loop: asyncio.AbstractEventLoop, - gesture_cls: type[Gesture], + gesture_instance: Gesture, event_instance: object, cooldown: float, last_trigger: list[float], ) -> callable: def callback(hand: object) -> bool: - gesture_detected = gesture_cls().detect(hand) + gesture_detected = gesture_instance.detect(hand) if gesture_detected: now = time.time() if now - last_trigger[0] > cooldown: @@ -60,7 +60,7 @@ def run( gesture_instance = gesture_cls() last_trigger = [0.0] - callback = _get_callback(loop, gesture_cls, event_instance, cooldown, last_trigger) + callback = _get_callback(loop, gesture_instance, event_instance, cooldown, last_trigger) while True: ret, frame = camera.read() @@ -91,6 +91,12 @@ def run( if not headless: cv2.destroyAllWindows() detector.close() + + pending = asyncio.all_tasks(loop) + for task in pending: + task.cancel() + loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True)) + loop.close()