[Tutor] using Python with time
Tim Peters
tutor@python.org
Tue, 24 Apr 2001 00:18:44 -0400
[rob@jam.rr.com]
> I'd like to write a Python app that I can run "in the background" in
> both Windows and Linux/BSD/*nix, although it's hardly mission-critical
> that this be cross-platform.
The good news is that it will be cross-platform without even trying.
> What I'd like it to do is notify me at certain times of day (say,
> at 10:32 a.m. and 3:40 p.m.) or after a given interval of time (say,
> every XYZ minutes from the moment the app is activated).
>
> My practical aim for it is to have a way to remind myself to practice
> mindfulness every so often during the day, but I'm also curious how to
> handle the timing part of the code. If anyone could recommend where I
> can look on the timing part, I'll credit helpful folk when I post the
> code to Useless Python!</bribe>
Everything you need is in the time module, so read the docs for that. I'm
afraid the facilities in the time module are based closely on a horrible old
Unix interface for dealing with time, so there's not a lot there that will
make any sort of intuitive sense. This is where an interactive shell is
*essential*, so you can play with the time functions until what they really
do "clicks" for you:
>>> import time
>>> time.time()
>>> print time.time()
988084770.06
>>> # and about 6 seconds later
>>> print time.time()
988084776.32
>>>
So that's your first tool: time.time() returns a number that goes up by one
each second. So if you want to wait, say, for 3 minutes "from now", here's
one (but bad!) way to do that:
now = time.time()
while time.time() < now + 3*60:
pass
That's a really bad way to do it because it will keep your CPU busy the
entire time, running time.time() just as fast and often as it can.
The other tool you need is time.sleep():
>>> time.sleep(5)
>>>
You have to *try* that to get anything useful out of the example <wink>:
what happens is that, after hitting ENTER, the interactive shell just appears
to freeze for 5 seconds before you get a prompt back. Python's time.sleep(x)
is a portable way to tell the operating system "I don't want to do *anything*
for the next x seconds -- please just ignore me and do other stuff until x
seconds have elapsed, then please remember to wake me up again". This lets
you wait a long time without interfering with other programs.
So if you've got N seconds remaining to wait, time.sleep(N) will put your
program to sleep for *about* N seconds. It's better to sleep a little less
than that, because the OS may not wake you up *exactly* when N seconds have
elapsed, and the longer your program has been sleeping, the more likely that
the OS has swapped it out of main memory and onto disk. Then, depending on
your system, it make take the OS a couple seconds just to read your program
back into memory again so it can continue running it!
That's all nasty details, though. To a reasonably good first approximation,
if you want your program to wait for an hour, the one-liner
time.sleep(60*60)
will get pretty close. So start with that, then play around with the
combination of that and a "busy loop" (the "really bad" method above -- which
isn't bad at all when waiting for very *short* periods of time) to imporve
the accuracy.