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?

The time it takes to do something is not fixed. It takes as long as it takes. But you need both to be the same, so just make them the same by checking both. This code does the desired amount of work, and then calls time.sleep() until the remaining time block runs out. That's way better than looping and checking time() repeatedly, because looping and checking time makes the cpu work hard to do nothing. time.sleep is specifically there to avoid using the cpu.

Obviously, if you try to do more than you can in the specified time, you won't. Maybe you can return an error if that happens? If you check time() during the function you can exit early, but I would not recommend checking it too often.

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