Python - creating a delay that takes in account the execution time

548 views Asked by At

i am writing a function that reads N (800*600) frames from a bin file in one sec. the FPS is user controllable

import time

def getFrame(file,N):  #read N frames per second, 800X600 pixels each from file
    start = time.time()
    delay = (1/float(N))
    while N:
        frame = file.read(480000)
        time.sleep(delay)
        N -= 1
    print time.time()-start
    return

the time.sleep() creates the delay, but without taking in account the execution time of the rest of the code. so in total the running time is always bigger then 1 sec and it increments as i increase my N (fps) value.

is there a way to create a delay that takes into account the processing time ?

1

There are 1 answers

0
Joran Beasley On BEST ANSWER

ummm .... you need basic math here

time.sleep(delay-(time.time()-start))