vendredi 10 juin 2016

Run a function for a duration of 5 secs exactly every time using time.time() in python


I have defined a function record which appends value to a list named as lists for a duration of Time.Whenever I call this function for the first time it appends 19 values to lists,but when I call it second time it appends 20 values to lists even though the time parameter that I was using both the times was 1 sec.Is there any way that I could get same no of values get appended to a list when I call the function multiple times?

import time
lists=[]
first_list=[]
second_list=[]
def record(lists,Time):
    start_time=time.time()
    print "start_time",start_time
    print "time.time",time.time()
    while(time.time()-start_time)<=Time:

            lists.append(1)
            print "list",lists
            print "length of list after appending",len(lists)



record(first_list,0.05)#first time
print 1
time.sleep(5)
record(second_list,0.05)  #second time 

Aucun commentaire:

Enregistrer un commentaire