#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Exit a threading


    Hello!

    Could someone perhaps tell me how you exit a thread in Python?

    So I have e.g.

    Code:
    my_thread = threading.Thread(target = my_target)
    my_thread.start()
        
    i = 0
    while i<5:
        time.sleep(1)
        print i
        i = i + 1
    
    < some command to exit my_thread>
    Thanks heaps!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by tomasdepomas
    Hello!

    Could someone perhaps tell me how you exit a thread in Python?

    So I have e.g.

    Code:
    my_thread = threading.Thread(target = my_target)
    my_thread.start()
    [...]
    Thanks heaps!
    Code:
    my_thread.join()
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Thanks, but I still have problems exiting. So my script looks something like this:

    Code:
    import threading
    import time
    j=0
    def while_loop():
        i=0
        while True:
            time.sleep(2)
            i += 1
            print i
    my_thread = threading.Thread(target = while_loop)
    my_thread.start()
    while j<5:
        time.sleep(1)
        j += 1
        print j
    my_thread.join()
    sys.exit(0)
    When the thread joins, the while_loop function keeps running. Any idea?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by tomasdepomas
    When the thread joins, the while_loop function keeps running. Any idea?
    Ah, join() will only wait for the thread to finish, not make it exit. There are ways to forcefully exit a thread, but I think it's better to do it in a controlled fashion.

    Basically, you check a boolean in the thread if it's supposed to keep running. When you want to exit the thread, you set that boolean to False in your main thread. Then the thread can exit gracefully and perform teardown actions if needed.

    Code:
    import threading
    import time
    
    j=0
    run_thread = True
    
    def while_loop():
        i=0
        while True:
            if not run_thread:
                # Do cleanup here, if needed                                                                                              
                return
            time.sleep(2)
            i += 1
            print "In thread: %d" % i
    
    my_thread = threading.Thread(target = while_loop)
    my_thread.start()
    
    while j<5:
        time.sleep(1)
        j += 1
        print "In main: %d" % j
    
    run_thread = False
    my_thread.join()
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    I’m definitely not an expert in threading, but wouldn’t the while_loop function be cleaner written like this:

    Code:
    def while_loop():
        global run_thread
        i = 0
        while run_thread:
            time.sleep(2)
            i += 1
            print("In thread: {0}".format(i))
        # Do cleanup here, if needed
        return
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    I used the thread module instead of threading module. This script does the job for me:

    Code:
    import thread
    import time
    import sys
    
    j=0
    
    def while_loop():
        i=0
        while True:
            time.sleep(2)
            i += 1
            print i
    
    thread.start_new_thread(while_loop,())
    
    while j<5:
        time.sleep(1)
        j += 1
        print j
    
    sys.exit(0)

IMN logo majestic logo threadwatch logo seochat tools logo