[Tutor] How to set a timer.

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 6 Jan 2001 15:19:03 -0800 (PST)


> Can anyone point me in the right direction with a problem I'm having.
> 
> What I want to do is read a web page on an internal server every 10 seconds.
> 
> So far I've can get the information I want from the web page working using
> 'urllib', but this is only a one time routine.

There are several ways to approach this.  One way is to use an infinite
loop.  The pseudocode for this would be:

###
while 1:
    read that web page
    wait for 10 seconds
###

Python provides the function time.sleep to let a program delay itself for
an interval.  You can read more about time.sleep() here:

    http://python.org/doc/current/lib/module-time.html

The only problem with this approach is that, as it stands, it doesn't let
us do anything else outside of this while loop, since we're perpetually
repeating that code.  I'm not sure if this is what you want.


Another alternative is in the "sched" module --- sched allows you to
schedule a fuction to be called every few times.  I haven't worked with it
myself yet, but the documentation appears to have a good example in
there.  Here's the link:

    http://python.org/doc/current/lib/module-sched.html


Good luck!