Is inifinite loop not a good practice?

Steve Holden steve at holdenweb.com
Mon Feb 20 02:54:33 EST 2006


Alvin A. Delagon wrote:
> 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!

Clearly no process written as an infinite look is expected to run 
forever - it will be brought down by system reboots, for example, 
without going into end-of-life issues.

The classic network server process is pretty much an infinite loop, 
though perhaps with a break condition actioned when the operator sets 
some flag with a command, and there's no real reason why you shouldn't 
cast your application as such a loop. You will need to do so with care, 
however.

The danger is that you will spend time that should really be allocated 
to other processes on the system checking for database updates. So you 
need to design your polling mechanism fairly carefully - perhaps it 
could sleep five seconds once it found there were no more records that 
needed processing before looking again, or some similar mechanism to 
ensure that the system doesn't spend too much time looking for changes.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list