Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5095

Re: NewBie Doubt in Python Thread Programming

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.04; 'string.': 0.04; 'wed,': 0.04; 'main()': 0.05; '__name__': 0.07; 'prints': 0.07; 'python': 0.07; 'called.': 0.09; 'executes': 0.09; 'none):': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; 'result.': 0.14; "'__main__':": 0.16; "'hello": 0.16; '(until': 0.16; 'concepts.': 0.16; 'from:addr:yahoo.com.ar': 0.16; 'instance)': 0.16; 'main():': 0.16; 'object).': 0.16; 'run(self):': 0.16; 'subject:Programming': 0.16; 'subject:Thread': 0.16; 'thread?': 0.16; 'threading': 0.16; 'programming': 0.20; 'runs': 0.24; 'java': 0.24; 'preferred': 0.26; "i'm": 0.26; 'object': 0.27; 'class': 0.29; 'seem': 0.30; 'parent': 0.31; 'spelling': 0.31; 'threads.': 0.31; 'does': 0.31; 'all,': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; '...': 0.32; 'execution': 0.33; 'reference': 0.34; 'header:X-Complaints-To:1': 0.34; 'difference': 0.35; 'there': 0.35; 'concepts': 0.35; 'that,': 0.35; 'print': 0.35; 'header:User-Agent:1': 0.35; 'quite': 0.36; 'some': 0.37; 'run': 0.37; 'thread': 0.38; 'but': 0.38; 'received:org': 0.38; 'active': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'count': 0.40; 'finished': 0.40; 'stopped': 0.40; 'header:Received:5': 0.40; 'simple': 0.60; 'give': 0.61; '2011': 0.62; 'assistance': 0.63; 'alive': 0.68; 'nothing.': 0.68; 'received:190': 0.69; '-0300,': 0.84; 'following.': 0.84; 'genellina': 0.84; 'gabriel': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Gabriel Genellina" <gagsl-py2@yahoo.com.ar>
Subject Re: NewBie Doubt in Python Thread Programming
Date Wed, 11 May 2011 04:31:50 -0300
References <BANLkTikr8fu1sDrfu2a-wyEN_KpVVNCdew@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-1; format=flowed; delsp=yes
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 190.2.4.25
User-Agent Opera Mail/11.10 (Win32)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1394.1305099079.9059.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 82.94.164.166
X-Trace 1305099080 news.xs4all.nl 81473 [::ffff:82.94.164.166]:35017
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5095

Show key headers only | View raw


En Wed, 11 May 2011 03:57:13 -0300, vijay swaminathan <swavijay@gmail.com>  
escribió:

> 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?

Because most of them have already finished by then. Your run() method  
executes quite fast. Make it take more time (maybe by adding  
time.sleep(1)) and you'll see 10 active threads.

> 2. how do I stop a thread? does it get automatically stopped after  
> execution
> ?

You don't; a trhread runs until the run() method exits. After that, the OS  
thread finishes. The Python object (a threading.Thread instance) is still  
alive (until the last reference to it disappears, as any other object).

> 3. Am I totally wrong in understanding the concepts.

I don't know...

> 4. what is the difference between active_count() and activeCount() since
> both seem to give the same result.

Nothing. active_count is the preferred Python spelling per PEP8;  
activeCount is the original Java spelling.

> 5. is there a way to find out if the thread is still active or dead?


Yes, use is_alive()

-- 
Gabriel Genellina

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: NewBie Doubt in Python Thread Programming "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-05-11 04:31 -0300

csiph-web