# HG changeset patch # User Atul Varma # Date 1315161388 0 # Node ID 7e11415cf2728f6f5fd5c585777c543e7687a270 # Parent f64af329930f9220ac9dbcbe78ba04f3a70a2f38 time spent waiting for processes to start/stop is reduced. diff -r f64af329930f -r 7e11415cf272 ProcessManager.py --- 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,