Re: Is inifinite loop not a good practice?

2006-02-20 Thread 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
-- 
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is inifinite loop not a good practice?

2006-02-20 Thread Alvin A. Delagon
Thanks for the quick heads up! The comparison between implementing an 
infinite loop and cron is great. I'm beginning to see cron as the better 
solution between the two specially during crash instances. I'll try to 
code the script using the two solutions and do some stress testing to 
determine which is better between those two.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is inifinite loop not a good practice?

2006-02-20 Thread Terry Reedy

Alvin A. Delagon [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 I've been hearing comments that infinite loop is a bad programming
 practice.

What is bad is an *unintended* loop that goobles cpu time while outputting 
nothing.  Example:

def fact(n):
  res = 1
  while n != 0:
res *= n
n -= 1
  return res

fact(-1)

Now imagine that you are paying $360/hr ($.10/sec) (for IBM mainframe time) 
and you should understand the bad reputation (and why jobs were submitted 
with a time limit!). ;-)

Terry Jan Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is inifinite loop not a good practice?

2006-02-20 Thread Roy Smith
In article [EMAIL PROTECTED],
 Terry Reedy [EMAIL PROTECTED] wrote:

 Alvin A. Delagon [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
  I've been hearing comments that infinite loop is a bad programming
  practice.
 
 What is bad is an *unintended* loop that goobles cpu time while outputting 
 nothing.  Example:
 
 def fact(n):
   res = 1
   while n != 0:
 res *= n
 n -= 1
   return res
 
 fact(-1)
 
 Now imagine that you are paying $360/hr ($.10/sec) (for IBM mainframe time) 
 and you should understand the bad reputation (and why jobs were submitted 
 with a time limit!). ;-)
 
 Terry Jan Reedy

Could be worse.  You could have written:

def fact(n):
   if n == 0:
  return 1
   else:
 return n * fact (n-1)

then you would have not only gotten zonked on CPU charges, but on memory 
charges as well :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is inifinite loop not a good practice?

2006-02-20 Thread Felipe Almeida Lessa
Em Seg, 2006-02-20 às 17:01 -0500, Roy Smith escreveu: 
 In article [EMAIL PROTECTED],
  Terry Reedy [EMAIL PROTECTED] wrote:
  def fact(n):
res = 1
while n != 0:
  res *= n
  n -= 1
return res
  
  fact(-1)
 
 Could be worse.  You could have written:
 
 def fact(n):
if n == 0:
   return 1
else:
  return n * fact (n-1)
 
 then you would have not only gotten zonked on CPU charges, but on memory 
 charges as well :-)

This is not worse. Sometime in the future the function will raise a
RuntimeError with maximum recursion depth exceeded and free all system
resources, but the other one (without recursive calls) will only grow
and grow and grow...

-- 
Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas.

  -- Sun Tzu, em A arte da guerra

-- 
http://mail.python.org/mailman/listinfo/python-list

Is inifinite loop not a good practice?

2006-02-19 Thread 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!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is inifinite loop not a good practice?

2006-02-19 Thread Steve Holden
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/

-- 
http://mail.python.org/mailman/listinfo/python-list