vendredi 21 octobre 2016

Python Test execute program [duplicate]

This question already has an answer here:

Is there a way to slot the run time of a function in Python. For example, we can slot the function that run in 1 second. If the function is not finished running in 1 second (enter infinite loop, the algorithm run too slow, etc) we can end the function and return False

My function do certain task but the race condition happens quite often(it is expected behavior) and I don't want the whole program wait for it to finish

def test_function(self):
    '''
    General task method
    '''
    try:
        # Start approach
        self.foo.startTask()
        # Wait for task to start
        while self.foo.TaskInProgress == False:
            time.sleep(0.2)

        # Wait for task to finish
        while self.foo.TaskInProgress == True:
            time.sleep(0.2)
        print "Task has finished."
        return True
    except:
        print "ERROR: Fail Task"
        return False

Aucun commentaire:

Enregistrer un commentaire