Is inifinite loop not a good practice?

S

Sybren Stuvel

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
 
A

Alvin A. Delagon

Greetings,

I have to write a python script that would continously monitor and
process a queue database. Once the script sees an unprocessed record it
will create a thread to process it otherwise it will do nothing. 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'm opted to run the script via crontab but I consider it as
my last resort. Do you have any suggestions on the design? Thanks in
advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top