tags:

views:

246

answers:

2

I'm using this recipe: http://code.activestate.com/recipes/278731/ on an Ubuntu server.

I make a daemon instance like this:

class MyDaemon(Daemon):
    def run(self):
        while True:
            try:      
                do_my_data_processing()
            except MySQLdb.OperationalError:
                # Sleep an extra 30 seconds if database is away.
                time.sleep(30)
            time.sleep(30)

The problem is that even while sleeping the daemon takes up almost all available CPU power.

What am I doing wrong?

+3  A: 

The posted code looks correct. Your error must be somewhere else. Put a print statement into the loop to make sure that it does sleep.

Aaron Digulla
A: 

Turns out the daemon wasn't sleeping. It was looping without sleeping 30 seconds between every turn. Thanks Aaron.

I fixed it by changing my code to this:

class MyDaemon(Daemon):
    def run(self):
        while True:
            try:      
                do_my_data_processing()
                time.sleep(30)
            except MySQLdb.OperationalError:
                # Sleep an extra 30 seconds if database is away.
                time.sleep(30)
Hobhouse
mighty strange...
Wahnfrieden