Is inifinite loop not a good practice?

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Mon Feb 20 02:52:44 EST 2006


Alvin A. Delagon enlightened us with:
> I have to write a python script that would continously monitor and
> process a queue database. [...] I've been planning to do an infinite
> loop within the script to do this but I've been hearing comments
> that infinite loop is a bad programming practice.

I think it's just fine. You could improve it a bit by using something
like:

class Monitor(Thread):
    def __init__(self, *args, **kwargs):
        Thread.__init__(self, *args, **kwargs)
        self.interrupted = False
        
    def run(self):
        while not interrupted:
            monitor()
    
    def interrupt(self):
        self.interrupted = True

> I'm opted to run the script via crontab but I consider it as my last
> resort.

The advantage there is a stability issue. If your program quits,
you've got a problem. Using crontab, the program is started over and
over again, so even if it crashes, it'll be restarted in time for the
next monitor run. Cron has been around for such a long time that the
chance of a crash is much less than with a freshly developed program.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
                                             Frank Zappa



More information about the Python-list mailing list