Commits

Cameron Simpson committed fd4c403

cs.timeutils: import and bugfix safe sleep function from cs,fileutils_tests

Comments (0)

Files changed (2)

lib/python/cs/fileutils_tests.py

 from .fileutils import compare, rewrite, lockfile, Pathname, \
                         file_property, make_file_property, \
                         make_files_property
-from .timeutils import TimeoutError
+from .timeutils import TimeoutError, sleep
 from .logutils import D
 
-def sleep(delay):
-  t0 = time.time()
-  time.sleep(delay)
-  elapsed = time.time() - t0
-  if elapsed < delay:
-    D("time.sleep(%ss) took only %ss", delay, elapsed)
-    sleep(delay - elapsed)
-
 class TestFileProperty(object):
   def __init__(self):
     self._test1_path = 'testfileprop1'

lib/python/cs/timeutils.py

     warning("ISOtime: fromtimestamp(%d).microsecond = %s", gmtime, dt.microsecond)
   return dt.isoformat()
 
+def sleep(delay):
+  if delay < 0:
+    raise ValueError("cs.timeutils.sleep: delay should be >= 0, given %g", delay)
+  t0 = time.time()
+  end = t0 + delay
+  while t0 < end:
+    delay = end - t0
+    time.sleep(delay)
+    elapsed = time.time() - t0
+    if elapsed < delay:
+      from cs.logutils import warning
+      warning("time.sleep(%ss) took only %ss", delay, elapsed)
+    t0 = time.time()
+
 if __name__ == '__main__':
   iso = '2012-08-24T11:12:13'
   print "iso = %r" % (iso,)