Killing subservient threads

J

jimzat

I am trying to create an app which will have a main window from which
the user will launch other (children) windows.

When I launch the child window I start a new thread which periodically
polls another device and updates the child window accordingly. When I
dismiss the child window the "polling" thread stays alive and then
crashes with an exception when trying to write to the now defunct
child window.

How can I handle this? I want to either 1) have the destructor for
the child window kill the spawned thread or 2) catch the exception
within the "polling" thread and see that the child window is gone and
then commit suicide.

I am an experienced embedded C programmer but have VERY little
experience with GUI programming and no experience in the Micro$oft
programming world.
 
G

Gabriel Genellina

I am trying to create an app which will have a main window from which
the user will launch other (children) windows.

When I launch the child window I start a new thread which periodically
polls another device and updates the child window accordingly. When I
dismiss the child window the "polling" thread stays alive and then
crashes with an exception when trying to write to the now defunct
child window.

How can I handle this? I want to either 1) have the destructor for
the child window kill the spawned thread or 2) catch the exception
within the "polling" thread and see that the child window is gone and
then commit suicide.

1) make the child window set a flag in the thread (let's say, t.terminate
= True). And make the polling thread check the flag periodically (you
possibly already have a loop there - just break the loop when you detect
that self.terminate became true)

2) catching an exception is as easy as enclosing the code in a try/except
block. And "commit suicide" is just "exit from the run() method".
 
K

koranthala

I am trying to create an app which will have a main window from which
the user will launch other (children) windows.

When I launch the child window I start a new thread which periodically
polls another device and updates the child window accordingly.  When I
dismiss the child window the "polling" thread stays alive and then
crashes with an exception when trying to write to the now defunct
child window.

How can I handle this?  I want to either 1) have the destructor for
the child window kill the spawned thread or 2) catch the exception
within the "polling" thread and see that the child window is gone and
then commit suicide.

I am an experienced embedded C programmer but have VERY little
experience with GUI programming and no experience in the Micro$oft
programming world.

thread.setDaemon(True)
Makes it a daemon thread which means that interpreter will not stay
alive if only that thread is alive.
 
J

jimzat

thread.setDaemon(True)
Makes it a daemon thread which means that interpreter will not stay
alive if only that thread is alive.

My main window is used to launch multiple children and therefore when
one is dismissed the interpreter will remain active.
 
J

jimzat

1) make the child window set a flag in the thread (let's say, t.terminate  
= True). And make the polling thread check the flag periodically (you  
possibly already have a loop there - just break the loop when you detect  
that self.terminate became true)

2) catching an exception is as easy as enclosing the code in a try/except  
block. And "commit suicide" is just "exit from the run() method".

Gabriel,

I am using the thread module and calling thread.start_new_thread
(...). If I use t=thread.start_new_thread(...) and later set
t.terminate, I get "AttributeError: 'int' object has no attribute
'terminate'"

What is the proper way to do this? I don't want to use globals as I
will have multiple child windows of various classes.
 
G

Gabriel Genellina

I am using the thread module and calling thread.start_new_thread
(...). If I use t=thread.start_new_thread(...) and later set
t.terminate, I get "AttributeError: 'int' object has no attribute
'terminate'"

Use the threading module instead, that provides a higher level interface.
If you originally had:

thread.start_new_thread(function, args)

then it becomes:

polling_thread = threading.Thread(target=function, args=args)
....
polling_thread.start()
What is the proper way to do this? I don't want to use globals as I
will have multiple child windows of various classes.

Use an attribute of this Thread instance. Either a simple boolean or a
Condition/Event object as Christian Heimes suggested.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top