[Tutor] Creating Parent and child threads

Venkatesh Babu venkatbabukr at yahoo.com
Thu Nov 27 05:10:04 EST 2003


I have a question related to thread programming in

In my program, I am creating a set of threads which I
call parent threads because each of them in turn is
spawning one or more threads (child threads), starting
all these child threads simultaneously and waiting for
these child threads to terminate.

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? The program code is
shown below:

import threading

class ChildThread(threading.Thread):

    task = None

    def __init__(self, task):
        self.task = task

    def run(self):

class ParentThread(threading.Thread):

    children = None

    def __init__(self, tasks):
         self.children = list([])
         for t in tasks:

    def run(self):
         for c in self.children:
         for c in self.children:

Given this, if I create say 3 parent threads each of
which spawn 3 child threads:

ex: for i in range(3):
        mts.append(ParentThread([f1, f2, f3])) # f1,
f2 and f3 are functions

    for t in mts:

then few threads execute properly and the remaining
aren't executing. their _Thread__started has a value 1
but then why are the threads not executing?

Please help me in finding out what is the mistake? and
sorry for long mail.

Thank you,

Do you Yahoo!?
Free Pop-Up Blocker - Get it now

More information about the Tutor mailing list