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?

Pushpak Dagade
  • 6,280
  • 7
  • 28
  • 41
  • What kind of timing precision requirements do you have? – thkala Jan 15 '11 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. – Hugh Bothwell Jan 15 '11 at 21:20

3 Answers3

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]()
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
  • 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. – Jim Lewis Jan 15 '11 at 20:01
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.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • 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... – Pushpak Dagade Jan 15 '11 at 19:20
  • 1
    @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. – Jim Lewis Jan 15 '11 at 19:56
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.

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
  • 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... – Pushpak Dagade Jan 15 '11 at 19:20
  • 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. – Marcus Whybrow Jan 15 '11 at 19:29