Sleeping in Python

A simple daemon is all I want. Fairly trivial when you think about it. Just an infinite loop that excepts a certain kill signal to shutdown gracefully, but that otherwise just loops continuously executing a certain tasks or series of tasks.

Most specifically, I am talking about consuming and executing messages off of a queue. Where the tasks to be executed are contained behind a function that lives on the queued message object. This daemon only knows that it is supposed to pull a message off the queue, execute message.start(), then rinse and repeat.

Well, the case where the messages execute very quickly an infinite loop might execute TOO quickly and artificially eat up system resources doing nothing but just looping. Sleeping as little as half a second and prevent consuming all available CPU cycles:

>>> while True:
...   a = "This is a test."

This while loop had the Python process on my machine eating up 98% to 100% CPU time.

>>> import time
>>> while True:
...   a = "This is a test."
...   time.sleep(0.5)

This while loop never even spiked the Python process to even 1% CPU time.

Another interesting note that if you are attempting to write output either to standard out or a log file, you'll want to flush the output buffer manually inside the loop, otherwise the output will just continue to buffer. Not sure why this is the case -- if it is intended behavior or a bug.

Example:

import time

for i in range(10):
    print i, 
    sys.stdout.flush()
    time.sleep(1)

Without the sys.stdout.flush() the output is not printed to the terminal until the loop finishes and then it's all at once, not printed 1 second at a time.