Python code execution

I want to execute part of my code exactly at the same time every time I execute it, something like playing a media file ... (the same piece of code is executed exactly the same amount of time each time)

Is this possible in python?

+3
source share
3 answers

This should do the trick:

def run_with_delay(funcs, interval):
    for f in funcs[:-1]:
        before = time()
        f()
        # compensate the interval with the execution time.
        # NB: careful for functions that have a greater
        #     execution time than interval
        after = time()
        if after - before < interval:
            sleep(interval - (after - before))
    # last function is taken separately because we don't need
    # an extra useless sleep
    funcs[-1]()
+2
source

I do not think that this can be guaranteed by the construction of the language (in any language) - you must be in the real-time operating system. I believe that multimedia applications use device-level buffering to compensate for jitter in the OS process scheduler.

+1
source

, , .

, , .

0
source

Source: https://habr.com/ru/post/1785646/


All Articles