Terminating a thread from the parent

D

DE

Hello,

I have an app with embedded Python. Python scripts create their own
threads and I need to terminate these threads at the point where the
user wants to leave the application. I use threading.Thread as base
classes.

I have tried to use call the join method of the python thread objects
from C++. But although the call succeeds, the threads don't exit.

What is the proper way of doing this ? (e.g. how does the python shell
do this ? )

Thanks in advance,

Devrim.
 
P

Peter Hansen

DE said:
I have an app with embedded Python. Python scripts create their own
threads and I need to terminate these threads at the point where the
user wants to leave the application. I use threading.Thread as base
classes.

I have tried to use call the join method of the python thread objects
from C++. But although the call succeeds, the threads don't exit.

join() waits until the thread terminates, but it doesn't cause it to
terminate.
What is the proper way of doing this ? (e.g. how does the python shell
do this ? )

You have to poll for a termination request in the thread's main loop,
and have the thread terminate itself (by returning from the target
function or from the run() method, depending on which technique you used
to create the Thread in the first place). Threads cannot be forcibly
terminated**.

There are numerous examples of this in the archives and probably a
Cookbook recipe or two about it, if you look. Otherwise someone can
post an example here.

-Peter

** The exception is Threads on which .setDaemon(True) has been called,
which will terminate immediately when the main thread exits (i.e. when
the entire process terminates), though that might not be helpful to you
in your particular situation.
 
F

flupke

DE said:
Hello,

I have an app with embedded Python. Python scripts create their own
threads and I need to terminate these threads at the point where the
user wants to leave the application. I use threading.Thread as base
classes.

I have tried to use call the join method of the python thread objects
from C++. But although the call succeeds, the threads don't exit.

What is the proper way of doing this ? (e.g. how does the python shell
do this ? )

Thanks in advance,

Devrim.

I found this example somewhere. It shows how you terminate a thread.
As Peter said, it's the thread that terminates itself.

#!/usr/bin/env python
"""
testthread.py
An example of an idiom for controling threads

Doug Fort
http://www.dougfort.net
"""

import threading

class TestThread(threading.Thread):
"""
A sample thread class
"""

def __init__(self):
"""
Constructor, setting initial variables
"""
self._stopevent = threading.Event()
self._sleepperiod = 1.0

threading.Thread.__init__(self, name="TestThread")

def run(self):
"""
overload of threading.thread.run()
main control loop
"""
print "%s starts" % (self.getName(),)

count = 0
while not self._stopevent.isSet():
count += 1
print "loop %d" % (count,)
self._stopevent.wait(self._sleepperiod)

print "%s ends" % (self.getName(),)

def join(self,timeout=None):
"""
Stop the thread
"""
self._stopevent.set()
threading.Thread.join(self, timeout)

if __name__ == "__main__":
testthread = TestThread()
testthread.start()

import time
time.sleep(10.0)

testthread.join()

Benedict
 
D

DE

I appreciate your posts guys. It answers my questions and I like the
idea of overriding join method. I will use this one.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top