473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python Threads - stopped vs. gone vs. Alive

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
3 5292
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Matt Leslie | last post by:
Hi, I'm trying to use microthreads under stackless python, since they sound like exactly what I am after, but I am having very little success. I've got a fresh install of python 2.3.3 from...
20
by: greg.johnsen80 | last post by:
I have been struggling with the following problem: In my console application I have function1 calling function2 which in turn uses threads to do computationally intensive tasks. Some of the...
2
by: Mike | last post by:
Hi, I'm writing client-server application in Python. It's monitoring system, where server listen and waits for TCP connections, and every connection takes own thread. Every thread puts data from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.