Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4042352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:59:41+00:00 2026-05-20T12:59:41+00:00

Possible Duplicate: Is there any way to kill a Thread in Python? So this

  • 0

Possible Duplicate:
Is there any way to kill a Thread in Python?

So this question is a follow up to a solution posted previously. Basically it deals with programatically terminating a thread: http://sebulba.wikispaces.com/recipe+thread2

However it doesnt work… and I was wondering if someone can explain how one could terminate a thread that is blocking? My only guess is the fact that I am not providing the right thread id, but I did some testing and am pretty sure I can just use iden

If it is the thread ID, how can I go about getting the correct Thread ID?

Test Code:

class BlockingTestThread(Thread):

    def __init__(self):
        self._running_flag = False
        Thread.__init__(self, target=self.test_method)

    def test_method(self):
        try:
            while(not self.stopped()):
                self._running_flag = True
                time.sleep(100)
        finally:
                self._running_flag = False


def _async_raise(tid, exctype):
    '''Raises an exception in the threads with id tid'''
    if not inspect.isclass(exctype):
        raise TypeError("Only types can be raised (not instances)")

    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
    time.sleep(0.1)



if __name__ == "__main__":
    thread = BlockingTestThread()
    thread.start()

    _async_raise(thread.ident, SystemExit)
    print "Joining thread"
    thread.join()
    print "Done Joining thread"
    #will never get here!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:59:42+00:00Added an answer on May 20, 2026 at 12:59 pm

    Here is a better way to do it, use the “wait” command on the event, asssuming you want to use sleep.

    class BlockingTestThread(Thread):
        def __init__(self):
            self._running_flag = False
            self.stop  = threading.Event()
            Thread.__init__(self, target=self.test_method)
    
        def test_method(self):
            try:
                while(not self.stop.wait(1)):
                    self._running_flag = True
                    print 'Start wait'
                    self.stop.wait(100)
                    print 'Done waiting'
            finally:
                    self._running_flag = False
    
        def terminate(self):
             self.stop.set()  
    
    if __name__ == "__main__":
        thread = BlockingTestThread()
        thread.start()
    
        time.sleep(2)
        print 'Time sleep 2'
        thread.terminate()
        print "Joining thread"
        thread.join()
        print "Done Joining thread"
    

    Obviously you are going to need to wrap your blocking thread in something that uses the pattern above, but if you can’t the other option is to cause your process to throw an exception, in our case we basically kill the underlying connection, which causes an exception, and when that exception happens if the stopped flag is set, we ignore it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there any way in C# to override a class method with
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Possible Duplicate: Java Regex Replace with Capturing Group Is there any way to replace
Possible Duplicate: Catch multiple Exceptions at once? Is there any way in C# to
Possible Duplicate: Javascript callback when IFRAME is finished loading? Is there any way to
Possible Duplicate: is there any other way of creating an object without using “new”
Possible Duplicate: .trim() in JavaScript not working in IE Is there any way to
Possible Duplicate: Java REPL shell Hey, Is there any way to execute Java code
Possible Duplicate: How can I obfuscate JavaScript? Hi there, is there any way to
Possible Duplicate: Reference: Comparing PHP's print and echo Is there any major and fundamental

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.