473,402 Members | 2,061 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,402 software developers and data experts.

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

Similar topics

0
by: Marco Nicosia | last post by:
Hello gang, My coworker and I are writing a Python class for the other developers within our team. This class is supposed to encapsulate several things, including daemonizing, setting up...
2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
2
by: Byron | last post by:
I'm new to C# and threading, so hopefully this is a simple newbie question. I have a form that is supposed to listen for network traffic on a given port and decode and display any interesting...
3
by: Kranthis | last post by:
Hi, I want to restart a thread after same variable value in the thread reaches 100 in c# . How can I do. Please suggest me any solution
7
by: Ram | last post by:
Hi, I have two threads, one parent thread containing UI and a child thread waiting for some events from the parent thread. Now, I have two issues: 1) to keep the child thread active till the end...
23
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an...
6
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
18
by: Adda | last post by:
I have an mdi form and I am trying to call a procedure - "myTest" in the parent from the childform. I am experimenting using a thread, but the thread is not releasing the childform. I place a...
11
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.