Kill a thread in Python

Discussion in 'Python' started by C. Barnes, May 4, 2004.

  1. C. Barnes

    C. Barnes Guest

    This code module allows you to kill threads. The
    class KThread is a drop-in replacement for
    threading.Thread. It adds the kill() method, which
    should stop most threads in their tracks.

    #
    ---------------------------------------------------------------------
    # KThread.py: A killable Thread implementation.
    #
    ---------------------------------------------------------------------

    import sys
    import trace
    import threading

    class KThread(threading.Thread):
    """A subclass of threading.Thread, with a kill()
    method."""
    def __init__(self, *args, **keywords):
    threading.Thread.__init__(self, *args, **keywords)
    self.killed = False

    def start(self):
    """Start the thread."""
    self.__run_backup = self.run
    self.run = self.__run # Force the Thread to
    install our trace.
    threading.Thread.start(self)

    def __run(self):
    """Hacked run function, which installs the
    trace."""
    sys.settrace(self.globaltrace)
    self.__run_backup()
    self.run = self.__run_backup

    def globaltrace(self, frame, why, arg):
    if why == 'call':
    return self.localtrace
    else:
    return None

    def localtrace(self, frame, why, arg):
    if self.killed:
    if why == 'line':
    raise SystemExit()
    return self.localtrace

    def kill(self):
    self.killed = True


    ------------------------------------------------------------------------
    Example usage:
    ------------------------------------------------------------------------

    This illustrates running a function in a separate
    thread. The thread is
    killed before the function finishes.

    from KThread import *

    def func():
    print 'Function started'
    for i in xrange(1000000):
    pass
    print 'Function finished'

    A = KThread(target=func)
    A.start()
    for i in xrange(1000000):
    pass
    A.kill()

    print 'End of main program'


    Output:

    Function started
    End of main program


    -----------------------------------------------------------------------
    How It Works:
    -----------------------------------------------------------------------

    The KThread class works by installing a trace in the
    thread. The trace
    checks at every line of execution whether it should
    terminate itself.
    So it's possible to instantly kill any actively
    executing Python code.
    However, if your code hangs at a lower level than
    Python, then the
    thread will not actually be killed until the next
    Python statement is
    executed.

    -----------------------------------------------------------------------
    Considerations:
    -----------------------------------------------------------------------

    Certain IDEs may install their own threading trace
    code, for debugging
    purposes. This module is incompatible with those
    IDEs.

    Consider bugging the Python folks, until they give us
    a thread kill
    method. The code has already been written:

    http://mail.python.org/pipermail/python-list/2003-February/148999.html


    Regards,
    Connelly Barnes





    __________________________________
    Do you Yahoo!?
    Win a $20,000 Career Makeover at Yahoo! HotJobs
    http://hotjobs.sweepstakes.yahoo.com/careermakeover
     
    C. Barnes, May 4, 2004
    #1
    1. Advertisements

  2. C. Barnes

    Roger Binns Guest

    That is the usual case for me. My threads are blocked in calls
    to read, accept or Queue.get().
    Looking through that patch I couldn't tell if it could
    terminate a thread that was in native code. It seemed
    to cause the kill to happen in the next bit of Python
    bytecode interpretted.

    Incidentally Java does have a thread kill method although
    it is strongly recommended against using.

    Roger
     
    Roger Binns, May 4, 2004
    #2
    1. Advertisements

  3. C. Barnes

    Peter Hansen Guest

    Although I'm usually against premature optimization, in this
    case I have to point out the potentially *very* significant
    impact of using the system tracing capability...

    Also, this sort of "kill" is easily implemented on a special
    case basis in real code, where you generally just want to check
    at a high level whether the thread has been requested to die.
    Doing it at each bytecode seems like overkill, unless you
    tend to write lots of potentially endless loops at lower levels.

    Having a thread stuck in external code is by far the most common
    use case for this, I think, which this won't handle.

    It's a nice example of a use of sys.settrace, however...

    -Peter
     
    Peter Hansen, May 4, 2004
    #3
  4. Antoon Pardon, May 5, 2004
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.