[pygtk] gobject.timeout_add timeout recalculation

skip at pobox.com skip at pobox.com
Fri May 16 07:48:22 WST 2008


    Mitko> However, I am having a bit of trouble understanding exactly how
    Mitko> the timeout is calculated. According to the documentation:

    Mitko>      After each call to the timeout function, the time of the
    Mitko>      next timeout is recalculated based on the current time and
    Mitko>      the given interval (it does not try to 'catch up' time lost
    Mitko>      in delays).

    Mitko> The "After each call to the timeout function,..." leads me to
    Mitko> think that the timer is reset when the timeout function returns.
    Mitko> However, the phrase "(it does not try to 'catch up' time lost in
    Mitko> delays)." makes me think that the timer is reset as soon as the
    Mitko> timeout function thread is started. (This also seems to be
    Mitko> confirmed by tracing my code)

I believe it works like this:

    set wakeup for X milliseconds from now
    call your callback
    if it returned true:
        set wakeup for X milliseconds from now
    call your callback
    if it returned true:
        set wakeup for X milliseconds from now
    call your callback
    if it returned true:
        set wakeup for X milliseconds from now
    ...

If X is 2500ms and your callback takes 2ms to execute, the interval between
successive calls will actually be 2502ms.
    
    Mitko> What I would like to happen is that the timeout gets recalculated
    Mitko> when my timeout function is done/returns. I guess, I could add a
    Mitko> new timeout from within my timeout function and have it [the
    Mitko> timeout function] return False (so the old timeout is destroyed)
    Mitko> but I am thinking that there has to be a better way to do this.

In our applications we added an abs_timeout_add function which takes hour,
minute, second and microsecond.  The callback is called at the same time
each day by taking into account the runtime of the callback.  You could do
something similar with a shorter period.

Skip



More information about the pygtk mailing list