Start and stop a thread in Python

The threading library can be used to execute any Python callable in its own thread. To do this, create a Thread instance and supply the callable that you wish to execute as a target as shown in the code given below –

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Code to execute in an independent thread
import time
  
def countdown(n):
    while n > 0:
        print('T-minus', n)
        n -= 1
        time.sleep(5)
          
# Create and launch a thread
from threading import Thread
t = Thread(target = countdown, args =(10, ))
t.start() 

chevron_right


When a thread instance is created, it doesn’t start executing until its start() method (which invokes the target function with the arguments you supplied) is invoked. Threads are executed in their own system-level thread (e.g., a POSIX thread or Windows threads) that is fully managed by the host operating system. Once started, threads run independently until the target function returns.
 
Code #2 : Querying a thread instance to see if it’s still running.



filter_none

edit
close

play_arrow

link
brightness_4
code

if t.is_alive():
    print('Still running')
else:
    print('Completed')

chevron_right


One can also request to join with a thread, which waits for it to terminate.

filter_none

edit
close

play_arrow

link
brightness_4
code

t.join()

chevron_right


The interpreter remains running until all threads terminate. For long-running threads or background tasks that run forever, consider it making the thread daemonic.
 
Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

t = Thread(target = countdown, args =(10, ), daemon = True)
t.start()

chevron_right


Daemonic threads can’t be joined. However, they are destroyed automatically when the main thread terminates. Beyond the two operations shown, there aren’t many other things to do with threads. For example, there are no operations to terminate a thread, signal a thread, adjust its scheduling, or perform any other high-level operations. To have these features, build them on their own. To be able to terminate threads, the thread must be programmed to poll for
exit at selected points. For example, put your thread in a class such as the one mentioned in the code below –

Code #4 : Putting the thread in a class.

filter_none

edit
close

play_arrow

link
brightness_4
code

class CountdownTask:
      
    def __init__(self):
    self._running = True
      
def terminate(self):
    self._running = False
      
def run(self, n):
    while self._running and n > 0:
        print('T-minus', n)
        n -= 1
        time.sleep(5)
  
c = CountdownTask()
t = Thread(target = c.run, args =(10, ))
t.start()
...
# Signal termination
c.terminate() 
  
# Wait for actual termination (if needed) 
t.join() 

chevron_right


Polling for thread termination can be tricky to coordinate if threads perform blocking operations such as I/O. For example, a thread blocked indefinitely on an I/O operation may never return to check if it’s been killed. To correctly deal with this case, thread needs to be carefully programmed to utilize timeout loops as shown in the code given below.

Code #5 :

filter_none

edit
close

play_arrow

link
brightness_4
code

class IOTask:
    def terminate(self):
        self._running = False
          
        def run(self, sock):
            # sock is a socket
              
            # Set timeout period
            sock.settimeout(5
            while self._running:
                  
                # Perform a blocking I/O operation w/timeout
                try:
                    data = sock.recv(8192)
                    break
                except socket.timeout:
                    continue
                # Continued processing
                ...
        # Terminated
        return

chevron_right


Due to a global interpreter lock (GIL), Python threads are restricted to an execution model that only allows one thread to execute in the interpreter at any given time. For this reason, Python threads should generally not be used for computationally intensive tasks where trying to achieve parallelism on multiple CPUs. They are much better suited for I/O handling and handling concurrent execution in code that performs blocking operations (e.g., waiting for I/O, waiting for results from a database, etc.).
 
Code #6 : Threads defined via inheritance from the Thread class

filter_none

edit
close

play_arrow

link
brightness_4
code

from threading import Thread
  
class CountdownThread(Thread):
    def __init__(self, n):
        super().__init__()
        self.n = 0
          
    def run(self):
        while self.n > 0:       
    print('T-minus', self.n)
    self.n -= 1
    time.sleep(5)
      
c = CountdownThread(5)
c.start()

chevron_right


Although this works, it introduces an extra dependency between the code and the threading library. That is, only the resulting code can be used in the context of threads, whereas the technique shown earlier involves writing code with no explicit dependency on threading. By freeing your code of such dependencies, it becomes usable in other contexts that may or may not involve threads. For instance, one might be able to execute the code in a separate process using the multiprocessing module using code given below –

Code #7 :

filter_none

edit
close

play_arrow

link
brightness_4
code

import multiprocessing
c = CountdownTask(5)
p = multiprocessing.Process(target = c.run)
p.start()
...

chevron_right


Again, this only works if the CountdownTask class has been written in a manner that is neutral to the actual means of concurrency (threads, processes, etc).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.