From d2ce509320113e69f1af4ee91c931898c7a6eaed Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 10 Jan 2019 07:55:33 +0100 Subject: [PATCH] module: rework polling in NMUtil.wait_till_connection_is_gone() time.sleep() does not sleep an exact amount of time, hence, adding up the sleep-times will not give the exact total wait-time. An alternative would be to take a CLOCK_BOOTTIME timestamp (or similar) and determine the elapsed time based on that. Instead, do something different, and use a GLib timeout for polling. --- library/network_connections.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/library/network_connections.py b/library/network_connections.py index 835e416..1ce8a7d 100644 --- a/library/network_connections.py +++ b/library/network_connections.py @@ -1060,20 +1060,15 @@ class NMUtil: :returns: True when connection is gone, False when timeout elapsed :rtype: bool """ - wait_time = 0 - sleep_time = 0.1 - while True: - connections = self.connection_list(uuid=uuid) - if not connections: - return True - wait_time += sleep_time - if wait_time > timeout: - break - time.sleep(sleep_time) - Util.GMainLoop_iterate_all() + def _poll_timeout_cb(unused): + if not self.connection_list(uuid=uuid): + Util.GMainLoop().quit() - return False + poll_timeout_id = Util.GLib().timeout_add(100, _poll_timeout_cb, None) + gone = Util.GMainLoop_run(timeout) + Util.GLib().source_remove(poll_timeout_id) + return gone def connection_activate(self, connection, timeout=15, wait_time=None):