[Tutor] Re: Creating Parent and child threads

Lee Harr missive at hotmail.com
Thu Nov 27 10:14:48 EST 2003


>Now my problem is: The program works fine as long as
>there are few parent threads but as the number of
>parent threads becomes, more my program hangs. I guess
>a dead lock is happening but I'm not able to figure
>out what is causing this. Is it due to any mistake in
>the program or something else?
>

Not sure... works for me.

I rewrote your code a bit...


import threading


class ChildThread(threading.Thread):
    def __init__(self, task):
        threading.Thread.__init__(self)
        self.task = task

    def run(self):
        self.task()
        return

class ParentThread(threading.Thread):
    def __init__(self, tasks):
         threading.Thread.__init__(self)
         self.children = []
         for t in tasks:
             self.children.append(ChildThread(t))

    def run(self):
         for c in self.children:
             c.start()
         for c in self.children:
             c.join()
         return


def f1():
    print 'f1'


def f2():
    for c in range(2):
        print 'f2'


def f3():
    for c in range(3):
        print 'f3'


def main():
    PARENT_THREADS = 3
    CHILD_THREADS = 1

    mts = []
    for i in range(PARENT_THREADS):
        mts.append(ParentThread([f1, f2, f3]*CHILD_THREADS))

    for t in mts:
        t.start()


if __name__ == '__main__':
    main()



When I bumped PARENT_THREADS up to about 3000 I ran out of memory,
but with PARENT_THREADS at 300 and CHILD_THREADS at 100 it was able
to finish without deadlocking.

What are you actually doing in f1 f2 and f3. Maybe that is the problem...

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* 
http://join.msn.com/?page=features/junkmail




More information about the Tutor mailing list