I remember when I was pursuing my degree and the taught me about time and space complexity, so but with advanced technology, we have all the advanced hardware to work with so space complexity is not something we are worried about that much. So the only thing left is space complexity. 

I can remember how I was scraping some data from a website with python and it took a day to scrap all the data, I wish I had learnt about threading with python in the early stage of learning python. But as we all know doing late is a lot better than not doing it at all. So Here I am to teach you about what threading is and how you can minimize the time to execute your programme.

What is threading?

Before diving directly into code, we much learn what threading really is. Let's take an example here

if you give the task of washing cloaths He will do it like this

  • take cloth
  • put it in soapy water
  • wash it
  • clean it with clean water

So if we have 100 cloths then he has to repeat the process for all the 100 cloths, and let's take if it takes One minute to wash, then washing 100 cloth is gonna to take 100 minutes. So now imagine we have 2 men to do the same task, now each has 50 cloth to wash to the task will be completed in just 50 minutes, and if we have 10 men then it will take only 10 minutes. 

So here take washing cloth as any task, and man as machine power. As we are hiding powerful CPU under the hood. So we make more use of processing power. So two instance of the programme will complete the task at the same time, and that is defined as threading. 

Running more than one instance of the programme by using more CPU Power.

How to use thread in python

import threading 
def cube(num): 
    print("Cube: {}".format(num * num * num)) 
def square(num): 
    print("Square: {}".format(num * num)) 
# here we have created two threads t1 & t2 
t1 = threading.Thread(target=print_square, args=(10,)) 
t2 = threading.Thread(target=print_cube, args=(10,)) 
# starting t1
# starting t2


Here we have seen that threading is the class comes inbuilt with python, so you can import it with "import threading". 

After importing the module you will create a thread by threading. Thread and passing method name as the first parameter followed by method parameters.

Remember that this will not start the thread, to start the thread you will need to call start on the instances, So once you call to start the threads will be running. 

Threading is not the only library in python for threading, but you can also use thread by importing thread

import thread
import time

def print_time( thread_name, delay):
   count = 0
   while count < 5:
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )
# Create two threads as follows
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
   print "Error: unable to start thread"

The code is much simpler to explain itself. Try it out in your favaourat editor and let me know if you face any problem by putting the comments.

Source : https://smartsoftware247.com/python-multithreading-tutorial-concurrency-and-parallelism/
Continue Reading