468,316 Members | 2,068 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

How to Restart a thread

class mythread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name = threadname)

def run(self):
print 'i am running'
print 'i quit run()'

thread = mythread('1')
thread.start()
print threading.activeCount() ## 1 , this means the thread is active
if not thread.isAlive():
print 'i am dead,you can restart' ### OK, this is executed. this
means the thread is no alive
thread.start() #### " thread already started "

This program presentes that the thread quit the *run()* func and is
active but not alive. How to understand this? It is said if the
thread quit the *run()* func , it's dead.
how to restart it ?

Thanks

Nov 25 '06 #1
1 14986
many_years_after wrote in news:1164467660.760671.145440
@j44g2000cwa.googlegroups.com in comp.lang.python:
class mythread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name = threadname)

def run(self):
print 'i am running'
print 'i quit run()'

thread = mythread('1')
thread.start()
print threading.activeCount() ## 1 , this means the thread is active
if not thread.isAlive():
print 'i am dead,you can restart' ### OK, this is executed. this
means the thread is no alive
thread.start() #### " thread already started "

This program presentes that the thread quit the *run()* func and is
active but not alive. How to understand this? It is said if the
thread quit the *run()* func , it's dead.
how to restart it ?

Thanks
Form the manual [1] (you must have missed this):

start( )

Start the thread's activity.
This must be called at most once per thread object.
It arranges for the object's run() method to be invoked
in a separate thread of control.

Note the 2nd line:

This must be called at most once per thread object.

so you simply can't restart a Thread object.

[1] http://docs.python.org/lib/thread-objects.html

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kranthis | last post: by
2 posts views Thread by Jianren | last post: by
4 posts views Thread by am | last post: by
14 posts views Thread by iceman | last post: by
6 posts views Thread by Leonardo Curros | last post: by
22 posts views Thread by Brett | last post: by
5 posts views Thread by juky | last post: by
10 posts views Thread by Jon Slaughter | last post: by
5 posts views Thread by puggid | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.