473,287 Members | 1,651 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,287 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 5286
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.