Python or OS forking/threading problem?

Andrew M. Kuchling akuchlin at mems-exchange.org
Wed Mar 22 16:54:32 EST 2000


nsiam at yahoo.com writes:
> Running the Python program below results in either:
> i) Python segfaults (that is the parent process, see below)
> ii) One or more processes hogging all the CPU

Hmm... this is wacky.  Here's your test program, after fixing
Deja.com's mangling of the code:

import threading
import os, sys

class MyThread(threading.Thread):
    def run(self):
        for i in range(50):
            print 'calling once', i
            self.once()
        print 'Returning from run()'
        
    def once(self):
        print ' calling fork'
        pid = os.fork()
        print ' fork output', pid
        if pid == 0:
            print " hello mom"
            sys.stdout.flush()
            os._exit(0)
        print 'Calling waitpid', pid
        pid2, sts = os.waitpid(pid,0)
        print " bye baby"
        sys.stdout.flush()
            
threads = []
for i in range(10):
    threads.append(MyThread())
print threads
map(lambda x: x.start(), threads)
print 'All started'

import time
print 'Final sleep'
time.sleep(5)
print "DONE"

I've added debugging printouts and changed some of the numbers; run()
only loops 10 times instead of 100, and the final delay is only 5
seconds, not 1000.

On Solaris 2.6 and Python 1.5.2, it hangs very quickly:

[<MyThread(Thread-1, initial)>, <MyThread(Thread-2, initial)>, ... ]
calling once 0
 calling fork
 fork output 0
 hello mom
 fork output 19633
Calling waitpid 19633
 bye baby
calling once 1
 calling fork
 fork output 0
 hello mom
 fork output 19634
Calling waitpid 19634

Apparently hanging in waitpid... The current CVS tree on the same
machine, however, doesn't hang; it runs through the whole sequence.
Looking through the CVS logs, I can't find a relevant checkin, so I
don't know what caused the change.  So, try the current CVS tree and
see if that improves matters.  (But Zope won't work with the current
CVS tree; still, at least you can determine if the current CVS helps,
and then look for the precise bugfix.)

Can anyone suggest what's going on here?  

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
And how often do we meet the man who prefaces his remarks with: "I was reading
a book last night..." in the too loud, overenunciated fashion of one who might
be saying: "I keep a hippogryph in my basement." Reading confers status.
    -- Robertson Davies, _A Voice from the Attic_





More information about the Python-list mailing list