better scheduler with correct sleep times

Chris Rebert clp at rebertia.com
Sat Oct 18 16:39:45 EDT 2008


On Sat, Oct 18, 2008 at 5:09 AM, qvx <qvx3000 at gmail.com> wrote:
> I need a scheduler which can delay execution of a
> function for certain period of time.
> My attempt was something like this:
>
[code snipped]
>
> But then I came up with the following case:
>
> 1. I call delay with delay_sec = 10
> 2. The scheduler goes to sleep for 10 seconds
> 3. In the meantime (lets say 1 second later) I delay
>   another func but this time with delay_sec=0.5
> 4. The scheduler is sleeping and won't know call my
>   second function for another 9 seconds insted of 0.5
>
> I started googling for scheduler and found one in standard library
> but ih has the same code as mine (it calls the  functions in the
> right order and my doesn't, but it still waits too long).
> The other schedulers from web are dealing with
> repeating tasks and such.
>
> So, I wrote this:
>
[more code snipped]
>
> Is there a better way or some library that does that?

I believe you're looking for the 'sched' module:
http://www.python.org/doc/2.5.2/lib/module-sched.html

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>
> My observations:
>
> 1. Threading module uses time.sleep instead of time.clock
>   which results in less precise results (on windows platform)
>
>    if sys.platform=="win32":  #take care of differences in clock
> accuracy
>        wallclock = time.clock
>    else:
>        wallclock = time.time
>
> 2. while analyzing threading module i noticed that wait() is
>   implemented via loop and tiny sleep periods. I was expecting
>   the usage of underlaying OS primitives and functions but
>   then I remembered about GIL and quasi-multithreaded nature
>   of Python. But still, isn't there a more precise method
>   that interpreter itself could implement?
>
> Thanks,
> Tvrtko
>
> P.S. This was Python 2.5
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list