By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,532 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,532 IT Pros & Developers. It's quick & easy.

Threading-> Stopping

P: n/a
Is there a way to stop a thread with some command like t.stop()? Or any
other neat way to get around it? Thanks!

Nov 4 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Tuvas wrote:
Is there a way to stop a thread with some command like t.stop()? Or any
other neat way to get around it? Thanks!


Sadly, no. While Java and many other programming languages have an
interrupt() primitive, Python does not. You can approximate this by
using a global variable to tell the thread when to stop, for example:

shutdown = False

class MyThread(Thread):
def run(self):
while not shutdown:
# do whatever

def kill_thread():
shutdown = True

There's no general way to wake up a thread that's blocked--you have to
satisfy the condition that's causing it to block. If it's waiting for
input from a Queue, you have to push a dummy value down it to wake up
the thread and give it a chance to check the shutdown flag. If it's
blocking to do I/O, you'll have to use select() and provide a timeout
value to check the flag periodically.

-- David

Nov 4 '05 #2

P: n/a
Thanks!

Nov 4 '05 #3

P: n/a
Op 2005-11-04, Tuvas schreef <tu*****@gmail.com>:
Is there a way to stop a thread with some command like t.stop()? Or any
other neat way to get around it? Thanks!


What do you mean with stop? Pauze it or make it finish.

In the latter case, if you really want it you could use the following.
It requires ctypes. It also wont stop a thread while it is in a C
extention. In that case the exception will be raised when it returns
from the extention.

import os
import ctypes
from time import sleep
from random import randint
class TimeOut(Exception):
pass

class Alarm(Exception):
pass

import threading

class Xthread(threading.Thread):

def start(self):
self.__original_run = self.run
self.run = self.__run
threading.Thread.start(self)

def __run(self):
self.__thrd_id = threading._get_ident()
try:
self.__original_run()
finally:
self.run = self.__original_run

def raize(self, excpt):
Nr = ctypes.pythonapi.PyThreadState_SetAsyncExc(self.__ thrd_id, ctypes.py_object(excpt))
while Nr > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(self.__ thrd_id, None)
sleep(0.1)
Nr = ctypes.pythonapi.PyThreadState_SetAsyncExc(self.__ thrd_id, ctypes.py_object(excpt))

def alarm(self, tm):
alrm = threading.Timer(tm, self.raize, (TimeOut,))
alrm.start()
return alrm

if __name__ == "__main__":

class Continue(Xthread):

def run(self):

self.id = os.getpid()
print self.id, "Begin"
i = 0
try:
for _ in xrange(randint(0,20)):
for e in xrange(4 * 100000):
i = i + e
print self.id, "Finished"
except Alarm:
print self.id, "Interupted"

lst = [Continue() for _ in xrange(10)]

for T in lst:
T.start()

try:
sleep(10)
finally:
for T in lst:
T.raize(Alarm)
Nov 7 '05 #4

P: n/a
[Tuvas]
Is there a way to stop a thread with some command like t.stop()? Or any
other neat way to get around it? Thanks!


Good question.

And one that gets asked so often, I ask myself why it isn't in the FAQ?

http://www.python.org/doc/faq/library.html

It really should be in the FAQ. Isn't that what FAQs are for?

Maybe the FAQ needs to be turned into a wiki?

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Nov 7 '05 #5

P: n/a
On Mon, 07 Nov 2005 13:49:35 +0000, Alan Kennedy <al****@hotmail.com> wrote:
[Tuvas]
Is there a way to stop a thread with some command like t.stop()? Or any
other neat way to get around it? Thanks!


Good question.

And one that gets asked so often, I ask myself why it isn't in the FAQ?

http://www.python.org/doc/faq/library.html

It really should be in the FAQ. Isn't that what FAQs are for?

Maybe the FAQ needs to be turned into a wiki?

Maybe when really good answers to questions get posted, we could edit it
down to a final version candidate that we all agree on, and when agreed,
make the final post with a tag line that google will recognize and that
can be a tag line for python newsgroup gems.

Sort of like a wiki-within-newsgroup in effect. E.g., "Tim Peters" site:python.org
gets a lot of interesting stuff. Likewise Martelli, though the volume is daunting
either way (140,000 & 32,400 resp ;-)

What about "python-newsgroup-faq" site:python.org? If we avoid the tag line in all but
finalized posts, that plus some additional search-narrowing via google would probably
be pretty effective. But this is a social engineering problem more than technical ;-)

BTW, would such a thing appropriately be defined in a process PEP?

Regards,
Bengt Richter
Nov 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.