[Tutor] Re: Creating Parent and child threads

Venkatesh Babu venkatbabukr at yahoo.com
Thu Nov 27 13:36:13 EST 2003


Hi,

Unfortunately this modified code is also not working
here. I am not getting same output every time I
execute this piece of code. Thus, I've just pasted the
output
produced during one such executions.

The Output is shown below:

Note: I've saved the code in Test.py and imported Test
at the prompt.

Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import threading
>>> import Test
>>> f1
f3
f3
f3
f2
f2

>>> threading.enumerate()
[<_MainThread(MainThread, started)>,
<ParentThread(Thread-1, started)>,
<ParentThread(Thread-5, started)>,
<ChildThread(Thread-2, started)>,
<ChildThread(Thread-3, started)>,
<ParentThread(Thread-9, started)>,
<ChildThread(Thread-8, started)>]
>>>

These threads donot show any activity for long time
and I am finally stopping all these threads
explicitly. Also few parent threads aren't having all
their child threads created. Why is this happening?

Now, since this problem is not happening with you, is
this problem related to any configuration settings?

Regarding my implementation of functions f1, f2, f3:
They were also simple functions just printing some
arbitrary string.

Thank you,
Venkatesh

--- Lee Harr <missive at hotmail.com> wrote:
> 
> 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__ == 'Test':
>     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
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


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



More information about the Tutor mailing list