Python or OS forking/threading problem?

Neil Schemenauer nascheme at enme.ucalgary.ca
Wed Mar 22 22:45:02 EST 2000


Andrew M. Kuchling <akuchlin at mems-exchange.org> wrote:
>Hmm... this is wacky.

I don't know much about threading but here is my small
contribution.  The attached program locks up on my machine when I
increase the number of forking processes to more than one.  It
happens for the CVS version of Python as well as 1.5.2.

Also, there doesn't seem to be major changes to threading.py,
posix.waitpid or threadmodule.c between 1.5.2 and the current CVS
source.  Andrew, are you sure it doesn't lock up on Solaris?
Maybe you need to increase the number of threads.


    Neil


import threading
import os, sys
            
running = threading.Semaphore(20) # about 5 is enough on my machine
forking = threading.Semaphore(1) # more than 1 seems to cause deadlocks

class MyThread(threading.Thread):
    def start(self):
        running.acquire()
        threading.Thread.start(self)

    def run(self):
        print ' calling fork'
        forking.acquire()
        pid = os.fork()
        print ' fork output', pid
        if pid == 0:
            print " hello mom"
            sys.stdout.flush()
            os._exit(0)
        forking.release()
        print 'Calling waitpid', pid
        pid2, sts = os.waitpid(pid,0)
        print " bye baby"
        sys.stdout.flush()
        running.release()

while 1:
    t = MyThread().start()



More information about the Python-list mailing list