changeset 5:7e11415cf272

time spent waiting for processes to start/stop is reduced.
author Atul Varma <varmaa@toolness.com>
date Sun, 04 Sep 2011 18:36:28 +0000
parents f64af329930f
children a1e629ed86fc
files ProcessManager.py
diffstat 1 files changed, 26 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ProcessManager.py	Wed Mar 19 00:01:25 2008 +0000
+++ b/ProcessManager.py	Sun Sep 04 18:36:28 2011 +0000
@@ -329,10 +329,15 @@
             f.write( "%d" % pid )
             f.close()
 
-            time.sleep( POST_PROCESS_START_DELAY )
+            def isSuccessful():
+                try:
+                    retVal = os.waitpid( pid, os.WNOHANG )
+                except OSError:
+                    return False
+                return retVal == (0, 0)
 
-            retVal = os.waitpid( pid, os.WNOHANG )
-            if retVal == (0, 0):
+            time.sleep(0.5)
+            if _tryUntil(isSuccessful, POST_PROCESS_START_DELAY):
                 print "OK"
             else:
                 print "FAILED"
@@ -353,9 +358,10 @@
 
                 os.kill( pid, self.stopSignal )
 
-                time.sleep( POST_PROCESS_STOP_DELAY )
+                def isDone():
+                    return not _isPidRunning( pid )
 
-                if not _isPidRunning( pid ):
+                if _tryUntil(isDone, POST_PROCESS_STOP_DELAY):
                     print "OK"
                 else:
                     print "FAILED"
@@ -489,6 +495,21 @@
     commands = commands[:-1]
     return commands
 
+def _tryUntil(predicate, timeout):
+    """
+    Wait until predicate() returns true or the timeout (in seconds)
+    elapses, whichever comes first.
+
+    Returns True if predicate() returned True, False if the
+    timeout elapsed.
+    """
+
+    for i in range(0, int(timeout / 0.1)):
+        if predicate():
+            return True
+        time.sleep(0.1)
+    return False
+
 def rcScriptMain():
     """
     The main function of the rc-script use of the Process Manager,