[Tutor] what would happen if

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 5 Aug 2002 11:01:50 -0700 (PDT)


On Mon, 5 Aug 2002, Sean 'Shaleh' Perry wrote:

>
> On 05-Aug-2002 Kyle Babich wrote:
> > Out of curiosity what would happen if I were let something like:
> >
> > while 1:
> >     print "hello"
> >
> > run without stopping it?  Would the computer crash if I didn't stop it?
> >
>
> nope it would just cycle "forever".  It is fairly difficult to crash a
> machine via a mistake in python.


One reason that code above shouldn't cause too much problems is because,
by itself, it doesn't use up increasingly larger amounts of resources.

However, if we are running that loop in a program that collects and saves
the 'hello' lines in some file, it's possible to exhaust disk resources.
If we do something like:

###
C:\python\run_forever.py > saved_log_file.txt
###

in Windows, for example, the system will try to save all those 'hello'
lines in a file.

That being said, that infinite loop above is pretty benign: we can cancel
it by pressing 'Control-C', which causes Python to stop whatever it was
trying to do.



To make the idea more concrete, let's try something else.  Say we do
something like this:

###
bathtub = []
while 1:
    bathtub.append("drip")
###

Now, instead of leaving things the way they are, we're accumulating drips
in a bathtub.  That is, we can tell if we've run this loop more than once,
because the bathtub is growing.  This loop is one where we can say that it
uses more and more resources as it runs.  Eventually, if we let things
continue, we'll overflow the bathtub!


This is sorta handwavy, but I hope the idea makes sense.  *grin*