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

How can I kill a running thread when exiting from __main__

Hi!

I have the following problem: I have written a short Python server
that creates an indefinite simulation thread that I want to kill when
quitting (Ctrl-C) from Python. Googling around has not given me any
hints on how to cleanly kill running threads before exiting. Any help
is appreciated!

Carl

### CODE EXTRACT ###

import pythoncom

class QueueThread( threading.Thread):
def __init__(self, command):
threading.Thread.__init__(self)
self.command = command

def run(self):
pythoncom.CoInitialize()
try:
object = Dispatch('application')
execute = getattr(object, 'Execute')
execute(self.command )
finally:
object = None
pythoncom.CoUnitialize()

queuethread = QueueThread("queuehandler")
queuethread.setDaemon(True)
queuethread.start()

## How can I kill "queuethread" when exiting (Ctrl-C)?

Apr 3 '07 #1
5 10541
On Apr 3, 12:21 pm, car...@gmail.com wrote:
Hi!

I have the following problem: I have written a short Python server
that creates an indefinite simulation thread that I want to kill when
quitting (Ctrl-C) from Python. Googling around has not given me any
hints on how to cleanly kill running threads before exiting. Any help
is appreciated!

Carl

### CODE EXTRACT ###

import pythoncom

class QueueThread( threading.Thread):
def __init__(self, command):
threading.Thread.__init__(self)
self.command = command

def run(self):
pythoncom.CoInitialize()
try:
object = Dispatch('application')
execute = getattr(object, 'Execute')
execute(self.command )
finally:
object = None
pythoncom.CoUnitialize()

queuethread = QueueThread("queuehandler")
queuethread.setDaemon(True)
queuethread.start()

## How can I kill "queuethread" when exiting (Ctrl-C)?
If you can get the pid of the process you created, you should be able
to use the killProcName function that's included with the win32
package...if you're server is a Windows box. You should be able to use
the join() method if you don't set your thread to a Daemon.

This post talks about using the os module to kill a thread as well:

http://www.daniweb.com/techtalkforums/thread36752.html

The wxPython wiki has one of the best threading examples I've seen:
http://wiki.wxpython.org/index.cgi/LongRunningTasks

Mike

Apr 3 '07 #2
ca****@gmail.com wrote:
Hi!

I have the following problem: I have written a short Python server
that creates an indefinite simulation thread that I want to kill when
quitting (Ctrl-C) from Python. Googling around has not given me any
hints on how to cleanly kill running threads before exiting. Any help
is appreciated!

Carl

### CODE EXTRACT ###

import pythoncom

class QueueThread( threading.Thread):
def __init__(self, command):
threading.Thread.__init__(self)
self.command = command

def run(self):
pythoncom.CoInitialize()
try:
object = Dispatch('application')
execute = getattr(object, 'Execute')
execute(self.command )
finally:
object = None
pythoncom.CoUnitialize()

queuethread = QueueThread("queuehandler")
queuethread.setDaemon(True)
queuethread.start()

## How can I kill "queuethread" when exiting (Ctrl-C)?
The only way to do this is to have the thread regularly examine a
"please quit" flag that is set from the main thread when termination is
required.

There have been many suggestions to allow external forced termination of
threads, but this is impossible to specify in a clean way that is both
platform-independent and reliable, so it's not likely to happen.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 3 '07 #3

SteveThe only way to do this is to have the thread regularly examine a
Steve"please quit" flag that is set from the main thread when
Stevetermination is required.

I thought the process would terminate when only daemon threads were left.

Skip
Apr 3 '07 #4
sk**@pobox.com wrote:
SteveThe only way to do this is to have the thread regularly examine a
Steve"please quit" flag that is set from the main thread when
Stevetermination is required.

I thought the process would terminate when only daemon threads were left.

Skip
So it does - according to the documentation. In which case why is the OP
asking the question, since his code sets the queuethread daemonic before
starting it?

Maybe, then, the real answer is "just exit from the main thread"?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 3 '07 #5

SteveMaybe, then, the real answer is "just exit from the main thread"?

Yes, I believe so. If that's not what's happening I would look to see if
there is another thread which wasn't marked as a daemon.

Skip
Apr 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering...
7
by: martinnitram | last post by:
Dear all, in python, a thread can be created by t = threading.Thread. But i found that when the main (and the thread) program is running and user use Crtl+C/Crtl+Z to break the program abnormally,...
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...
6
by: RickDee | last post by:
Understand that when I start a thread, a number will be generated and is able to get from GetHashCode method. But I would like to use this number when I want to kill certain thread, anybody know...
1
by: John | last post by:
If my main UI thread starts a worker thread, who starts its own worker thread. So if i just close my app, in the closing event i can kill the thread the UI thread starts. but how can i kill the...
3
by: Stewart | last post by:
Hey Group, Hoping someone can help me out. I have some code which starts up some asynchronous code using a delegate. The code is below. Basically my main code (not shown) calls...
1
by: james_w77 | last post by:
I am trying to use Peter's StoppableThread(threading.Thread). What I want to do is to start 5 child threads, then do something, then when got ^C keyboard exception, stop the child thread. For...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
1
by: Laszlo Nagy | last post by:
Python threads are cooperative. I think there was a discussion about this, about a year ago or so. The answer was that in CPython, the interpreter has this GIL. Python threads are always running on...
0
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...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.