From 6455415eddb4b203b53f643df42eb4f84ab7fb13 Mon Sep 17 00:00:00 2001 From: Aljaz Date: Tue, 17 Jun 2025 15:15:27 +0200 Subject: [PATCH] Make wait_for_sync async --- main.py | 2 +- nodeless.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 8c54865..cf18c63 100644 --- a/main.py +++ b/main.py @@ -188,7 +188,7 @@ async def periodic_sync_check(): # Attempt resync with progressively longer timeouts based on consecutive failures timeout = min(5 + (_consecutive_sync_failures * 2), 30) # Increase timeout up to 30 seconds - if _payment_handler.wait_for_sync(timeout_seconds=timeout): + if await _payment_handler.wait_for_sync(timeout_seconds=timeout): logger.info("SDK resync successful") _last_sync_time = time.time() _consecutive_sync_failures = 0 diff --git a/nodeless.py b/nodeless.py index 2ece3b9..1ce7712 100644 --- a/nodeless.py +++ b/nodeless.py @@ -47,6 +47,7 @@ import time import logging from pprint import pprint import threading +import asyncio # Set up logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') @@ -281,7 +282,7 @@ class PaymentHandler: logger.info("Breez SDK connected successfully.") # Shorter sync timeout for initial connection - self.wait_for_sync(timeout_seconds=10) + self._wait_for_sync_blocking(timeout_seconds=10) except Exception as e: logger.error(f"Failed to connect to Breez SDK: {e}") @@ -290,9 +291,9 @@ class PaymentHandler: self._initialized = True logger.debug("PaymentHandler initialization complete") - def wait_for_sync(self, timeout_seconds: int = 10) -> bool: - """Wait for the SDK to sync before proceeding.""" - logger.debug(f"Waiting for sync (timeout={timeout_seconds}s)") + def _wait_for_sync_blocking(self, timeout_seconds: int = 10) -> bool: + """Blocking helper to wait for the SDK to sync before proceeding.""" + logger.debug(f"Waiting for sync (timeout={timeout_seconds}s) [blocking]") start_time = time.time() while time.time() - start_time < timeout_seconds: if self.listener.is_synced(): @@ -302,6 +303,18 @@ class PaymentHandler: logger.warning("SDK sync timeout") return False + async def wait_for_sync(self, timeout_seconds: int = 10) -> bool: + """Asynchronously wait for the SDK to sync before proceeding.""" + logger.debug(f"Waiting for sync (timeout={timeout_seconds}s) [async]") + start_time = time.time() + while time.time() - start_time < timeout_seconds: + if self.listener.is_synced(): + logger.debug("SDK synced successfully") + return True + await asyncio.sleep(0.1) + logger.warning("SDK sync timeout") + return False + def wait_for_payment(self, identifier: str, timeout_seconds: int = 60) -> bool: """ Wait for payment to complete or timeout for a specific identifier