From 7a48a7b69cee059fd44442eb1af627ec422561a0 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 24 Nov 2021 15:14:18 -0800 Subject: [PATCH] Let wait_for_internet take a backoff class to control sleeps. --- voussoirkit/networktools.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/voussoirkit/networktools.py b/voussoirkit/networktools.py index e6ae472..349cd18 100644 --- a/voussoirkit/networktools.py +++ b/voussoirkit/networktools.py @@ -61,11 +61,22 @@ def has_internet(timeout=2) -> bool: except socket.error as exc: return False -def wait_for_internet(timeout) -> None: +def wait_for_internet(timeout, *, backoff=None) -> None: ''' This function blocks until an internet connection is available, or the timeout is reached. + timeout: + Number of seconds after which we raise NoInternet. + + backoff: + You can provide an instance of a voussoirkit.backoff class to introduce + sleeps between each internet check. This can help reduce busywaiting at + the expense of not getting the earliest possible return. + + If None, there will be no sleeps between checks, though there is still + a timeout on each individual check. + Raises NoInternet if the timeout expires. ''' if timeout <= 0: @@ -73,7 +84,9 @@ def wait_for_internet(timeout) -> None: started = time.time() while True: + if has_internet(timeout=2): + return if time.time() - started >= timeout: raise NoInternet() - if has_internet(timeout=1): - return + if backoff is not None: + time.sleep(backoff.next())