Python interpreter hangs

Tim Peters at
Tue Dec 5 01:31:47 CET 2000

[posted & mailed]

[Rolf Wester]
> when I'm running the script that is listed below on one of our
> DEC-Alpha stations, python hangs after the print statement in
> the function test0.

How long did you wait for it to finish?

> The output "sum = 125000.0" is printed on the screen but the
> print statement at the end of the script is not executed.

So it reached the end of the function but hasn't returned from the function.
Between those two times, Python has to run around all over memory
decrementing refcounts on, and subsequently freeing, the approximately half
million temp array elements the function created.  That *could* take a long
time, depending on details of memory allocation, the platform libc, and the
platform's VM implementation.

> The process is not idle but is running and demands 100% of CPU-time.

Consistent with the above.

> Python does not react on Ctr C, I have to explicitely kill it.

Ditto, ditto.

> Curiously this script works on a NT-PC, on another DEC-Alpha
> station and it already worked on the same machine it's now hanging.

Wait longer before killing it; my guess is it will eventually finish if you
leave it alone.

> This is probably a problem with the DEC-Alpha station but  I
> don't have any idea what the reasons could be.  Did anybody have\
> a similar problem or  does anybody have an idea concerning my problem?

I don't think any of the Python developers use Alphas.  We've had weird
reports of supernaturally slow memory behavior on them before.  Until an
Alpha expert volunteers to dig into this stuff, though, it's likely to
remain A Mystery.

> import string
> def test0():
>  p  = []
>  iz = []
>  ix = []
>  iy = []
>  sum = 0.0
>  nn = 125001
>  heiz_dat0 = "12 23 33 1.0"
>  for i in range(1,nn):
>   zeile = string.split(heiz_dat0)
>   iz.append(zeile[0])
>   ix.append(zeile[1])
>   iy.append(zeile[2])
>   pf  = string.atof(zeile[3])
>   sum = sum + pf
>   p.append(pf)
>  print "sum = ", sum
>  return sum
> sum1 = test0()
> print "ende", sum1

One-at-a-time appending *may* be causing massive memory fragmentation.  You
could try preallocating the arrays and then indexing into them; e.g.,

iz = [None] * nn  # outside the loop
iz[i] = zeile[0]  # inside the loop

and similarly for the others.

More information about the Python-list mailing list