3

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

Is this possible in python?

2
  • What kind of timing precision requirements do you have? Commented Jan 15, 2011 at 19:18
  • 2
    Why must it always be the same time? Is it supposed to be synchronized with another event? It might be possible to synchronize them directly instead of by delay-time. Commented Jan 15, 2011 at 21:20

3 Answers 3

2

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]()
Sign up to request clarification or add additional context in comments.

1 Comment

If the application gets preempted by the OS while f() is executing, you're hosed -- unless you're on a real-time operating system, there's no guarantee that f() will complete within some bounded time interval.
1

I don't think this can be guaranteed by a language construct (in any language) -- you'd have to be on a real-time operating system. I believe multimedia applications take advantage of device-level buffering to compensate for timing jitter in the OS process scheduler.

2 Comments

Please elaborate what do you mean in your last line. Also if we can achieve this easily with media files, then i guess it should be able to achieve this possibly for any other code...
@Guanidene: Suppose you're playing a sound file with a sample rate of 44.1 kHz. The application, through some multimedia API, will queue up a batch of samples, at somewhat irregular intervals depending on how it gets scheduled by the OS. The output device, not the application or the operating system, is responsible for playing them out at the correct rate. So you're depending on the device to do the timing-critical part of the task, and this cannot be generalized to arbitrary code that doesn't have such hardware-level real time support.
0

I should think this would be impossible in an operating system which interleaves instructions to simulate simultaneous execution of multiple threads.

You would need a real-time library or language in order to stipulate deadlines for your code, and even then execution cannot be guaranteed in the allotted time.

2 Comments

Then what is the case with media files, they execute the same piece of code everytime in exactly the same time?? And this can be done by any simple program I guess...
A song is a collection of sounds, sampled at a certain frequency such that our ears perceive it to be a constant tune. Executing code is something that you ordinarily want to do a quickly as possible, what you may be leaning towards is event based programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.