NewBie Doubt in Python Thread Programming

vijay swaminathan swavijay at gmail.com
Wed May 11 02:57:13 EDT 2011


Hi All,

I'm new bie to thread programming and I need some assistance in
understanding few concepts ...

I have a very simple program which runs a thread and prints a string.

import threading

class MyThread(threading.Thread):
    def __init__(self, parent = None):
        threading.Thread.__init__(self)

    def run(self):
        print 'Hello World'

def main():
    for i in range(10):
        MyThread_Object = MyThread()
        print 'Object id is : ' , id(MyThread_Object)
        print 'Staring thread ----------> ' , MyThread_Object.getName()
        MyThread_Object.start()
    count = threading.activeCount()
    print 'The active thread count is: ' , count

if __name__ == '__main__':
    main()

When I run this, I could see 10 thread being called. But when I print the
active thread count it is only 2.

Need some understanding on the following.

1. How the total active thread is 2?
2. how do I stop a thread? does it get automatically stopped after execution
?
3. Am I totally wrong in understanding the concepts.
4. what is the difference between active_count() and activeCount() since
both seem to give the same result.
5. is there a way to find out if the thread is still active or dead?

Please help me in understanding ..

-- 
Vijay Swaminathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110511/56125357/attachment.html>


More information about the Python-list mailing list