Skip to content
Related Articles

Related Articles

Improve Article
Start and stop a thread in Python
  • Last Updated : 12 Jun, 2019

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 :




# 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() 

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.




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

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




t.join()

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 :






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

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.




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() 

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 :




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

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




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()

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 :




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

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).

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :