By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Python Threads - stopped vs. gone vs. Alive

P: n/a
I'm a newbie to python threads, and playing with some simple client
server stuff and lots of print statements.

My server thread launched with
self.worker = WorkerThread(self)
completes an interaction and then if I check on it's status with
print "Status:", self.worker I get Status none
A client thread behaves differently. Launched as
self.clientWorker( = ClientThreadself)
when it finishes it's work, I instead get:
Status: <ClientThread(Thread-2, stopped)>

If I check the isAlive status on each of those, self.worker.isAlive
throws an exception, 'cause there is nothing there anymore to check
isAlive on. But self.clientWorker comes back as False, since it is a
stopped thread and hasn't gone away (which I'd like it to after it
finishes its work).

So my question is when a thread finishes its work how do I more
predictably control whether it is just stopped, or goes away all
together? I don't want to do a double nested 'if' statement to check
if it exists before I check if it's alive.

Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 28, 12:01 pm, RossGK <ros...@gmail.comwrote:
I'm a newbie to python threads, and playing with some simple client
server stuff and lots of print statements.

My server thread launched with
self.worker = WorkerThread(self)
completes an interaction and then if I check on it's status with
print "Status:", self.worker I get Status none

A client thread behaves differently. Launched as
self.clientWorker( = ClientThreadself)
when it finishes it's work, I instead get:
Status: <ClientThread(Thread-2, stopped)>

If I check the isAlive status on each of those, self.worker.isAlive
throws an exception, 'cause there is nothing there anymore to check
isAlive on. But self.clientWorker comes back as False, since it is a
stopped thread and hasn't gone away (which I'd like it to after it
finishes its work).

So my question is when a thread finishes its work how do I more
predictably control whether it is just stopped, or goes away all
together? I don't want to do a double nested 'if' statement to check
if it exists before I check if it's alive.


Pls ignore the obvious typos, this isn't a syntax question and google
groups seems to be messing with my typing and spacing(!) e.g.
self.clientWorker( = ClientThreadself)
should have read
self.clientWorker = ClientThread(self)

As I swear I had typed it...
Jun 27 '08 #2

P: n/a

I've answered my own question about the "None" state - an event was
setting the thread to None where I didn't expect it.

However, my question slightly repositioned is if a Thread is "Stopped"
it still seems to exist. Is there someway to make it go away, send it
to garbage collection etc?

Other part of the original Q - I assume a Stopped thread gets a false
from "isAlive" and a Running thread gets a true?

Jun 27 '08 #3

P: n/a
On Wed, 28 May 2008 11:38:53 -0700, RossGK wrote:
>
I've answered my own question about the "None" state - an event was
setting the thread to None where I didn't expect it.

However, my question slightly repositioned is if a Thread is "Stopped"
it still seems to exist. Is there someway to make it go away, send it
to garbage collection etc?
You have to call the join() method of the thread object from another
thread. This will terminate the thread and free its resources. This is
usually the task of the parent thread which usually does something
like:
t = MyTread(...)
t.start()
# do your own stuff, then before quitting
t.join() # Note that this waits until t is terminated
Other part of the original Q - I assume a Stopped thread gets a false
from "isAlive" and a Running thread gets a true?
Yes. You can try yourself:
>>import threading>>class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def stop(self): self._stop = True def run(self): self._stop= False while self._stop == False: import time time.sleep(0.1) >>t = MyThread()>>t.start()>>t.isAlive()True
t.isAlive()False
t.join()>>t.isAlive()False

Ciao
----
FB
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.