[Tutor] How to use threads ?

Shitiz Bansal shitizb at yahoo.com
Wed Mar 2 23:48:11 CET 2005


The thread finishes when:
1.The run method finishes.
2.In case of the loop- you may -
    
>>> import threading
>>> class abc(threading.Thread):
	def __init__(self):
		threading.Thread.__init__(self)
		self.cont_flag=1
	def run(self):
		while self.cont_flag:
			pass

>>> abcd=abc()
>>> abcd
<abc(Thread-1, initial)>
>>> abcd.start()
>>> abcd
<abc(Thread-1, started)>
>>> abcd.cont_flag=0
>>> abcd
<abc(Thread-1, stopped)>

Also, you may 

>>> del abcd
>>> abcd

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in -toplevel-
    abcd
NameError: name 'abcd' is not defined

I am not very sure about the CPU usage of a stopped
thread.Perhaps someone from the group can enlighten
us.

Hope this makes it clear.

Cheers,
Shitiz
--- Mark Kels <mark.kels at gmail.com> wrote:

> On Tue, 1 Mar 2005 12:25:37 -0800 (PST), Shitiz
> Bansal
> <shitizb at yahoo.com> wrote:
> > 
> > Here is a simple program
> > 
> > class abc(threading.Thread):
> >     def __init__(self):
> >         threading.Thread.__init__(self)
> >         self.x=1 etc. etc.....
> >         self.cont_flag=1
> >     def run(self):
> >         while self.cont_flag:
> >            print self.x
> >            (you can set the self.cont_falg to zero
> >             whenever you wish to end the loop(and
> > hence
> >             the thread., either
> >             here, or even from outside the thread.
> >             of course, it isnt necessary that your
> >             thread is a loop, it can be a finite
> >             function.)
> > 
> Thanks alot !!
> I think I got it ths time.
> The only problem is that when I try to do it my
> thread doesnt closes.
> When does a thread closes ?
> 
> Thanks again. 
> 
> -- 
> 1. The day Microsoft makes something that doesn't
> suck is probably the
> day they start making vacuum cleaners.
> 2. Unix is user friendly - it's just picky about
> it's friends.
> 3. Documentation is like sex: when it is good, it is
> very, very good.
> And when it is bad, it is better than nothing. -
> Dick Brandon
> 



	
		
__________________________________ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/


More information about the Tutor mailing list