470,618 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

Terminating a thread from the parent

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.

Jul 19 '05 #1
3 19287
DE wrote:
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.
Jul 19 '05 #2
DE wrote:
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
Jul 19 '05 #3
DE
I appreciate your posts guys. It answers my questions and I like the
idea of overriding join method. I will use this one.

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marco Nicosia | last post: by
2 posts views Thread by raxitsheth | last post: by
3 posts views Thread by Kranthis | last post: by
7 posts views Thread by Ram | last post: by
23 posts views Thread by Adam Clauss | last post: by
6 posts views Thread by Tomaz Koritnik | last post: by
18 posts views Thread by Adda | last post: by
11 posts views Thread by seattleboatguy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.