AbstractPollTask: use timeout_add
authorZac Medico <zmedico@gentoo.org>
Wed, 8 Feb 2012 05:16:51 +0000 (21:16 -0800)
committerZac Medico <zmedico@gentoo.org>
Wed, 8 Feb 2012 06:08:45 +0000 (22:08 -0800)
pym/_emerge/AbstractPollTask.py

index b4728e7a242db5a4ea51e30dfdc75ecdaf792296..922905291d851a33bd23982b9b424a9a2410fc2b 100644 (file)
@@ -5,7 +5,6 @@ import array
 import errno
 import logging
 import os
-import time
 
 from portage.util import writemsg_level
 from _emerge.AsynchronousTask import AsynchronousTask
@@ -129,16 +128,14 @@ class AbstractPollTask(AsynchronousTask):
                                self.scheduler.iteration()
                        return
 
-               remaining_timeout = timeout
-               start_time = time.time()
-               while self._registered:
-                       self.scheduler.iteration()
-                       elapsed_time = time.time() - start_time
-                       if elapsed_time < 0:
-                               # The system clock has changed such that start_time
-                               # is now in the future, so just assume that the
-                               # timeout has already elapsed.
-                               break
-                       remaining_timeout = timeout - 1000 * elapsed_time
-                       if remaining_timeout <= 0:
-                               break
+               def timeout_cb():
+                       timeout_cb.timed_out = True
+                       return False
+               timeout_cb.timed_out = False
+               timeout_cb.timeout_id = self.scheduler.timeout_add(timeout, timeout_cb)
+
+               try:
+                       while self._registered and not timeout_cb.timed_out:
+                               self.scheduler.iteration()
+               finally:
+                       self.scheduler.unregister(timeout_cb.timeout_id)