[Tutor] Creating Parent and child threads

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


Hi,

I have a question related to thread programming in
Python.

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):
        threading.Thread.__init__(self)
        self.task = task

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

class ParentThread(threading.Thread):

    children = None

    def __init__(self, tasks):
         threading.Thread.__init__(self)
         self.children = list([])
         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

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:
        t.start()

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,
Venkatesh


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/



More information about the Tutor mailing list