468,511 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 10045
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Jerry Sievers | last post: by
7 posts views Thread by martinnitram | last post: by
reply views Thread by Marco Nicosia | last post: by
6 posts views Thread by RickDee | last post: by
1 post views Thread by John | last post: by
3 posts views Thread by Stewart | last post: by
1 post views Thread by james_w77 | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
1 post views Thread by Laszlo Nagy | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.