sleep and Timer

Peter Hansen peter at engcorp.com
Wed May 28 22:12:11 EDT 2003


Rob Hall wrote:
> 
> "Peter Hansen" <peter at engcorp.com> wrote in message
> > This way the thread wakes up every few seconds to check whether it should
> > terminate.  For small scripts, this might be good enough.
> 
> I did end up implementing something like that, the trouble is, it can still
> take up to 5 seconds for the app to die.  I could set it to sleep for 0.5
> seconds, but that seems to be a waste of processor time to me.

Hmm... been there...  allow me to offer two other perspectives:

1. "Waste of processor time"?  What's your processor planning
to do with all that time otherwise?  Really, "wasted" is probably
just a scary word for a fairly minor issue.  I wouldn't worry about it.
Besides, avoiding a 0.5 second sleep because of concerns about
performance when you probably don't really *need* the minor bit
of wasted time is "premature optimization" (i.e. a Bad Thing).
If you ever notice a problem, refactor this code and make it waste
less time, but until then you've got a working, useful program
with less effort.

2. Five seconds isn't usually a problem, in my experience.
Neither is two, or one, which are other values I often use for
things like this.

We've got one large app which has about seven threads running 
simultaneously, each of which wakes up about once every second to
check whether it should terminate.  We've never really noticed
that there was a problem, either with the one second delay being
too long, nor with any wasted processor time.

Also, if this is just a small script or minor application, you
likely won't ever notice an issue with a 0.5 or 1.0 second wakeup.

-Peter




More information about the Python-list mailing list