How to Restart a thread

M

many_years_after

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
 
R

Rob Williscroft

many_years_after wrote in @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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,271
Latest member
BuyAtenaLabsCBD

Latest Threads

Top