python3 create thread code example

Example 1: create new thread python

from threading import Thread
from time import sleep

def threaded_function(arg):
    for i in range(arg):
        print("running")
        sleep(1)


if __name__ == "__main__":
    thread = Thread(target = threaded_function, args = (10, ))
    thread.start()
    thread.join()
    print("thread finished...exiting")

Example 2: multithread python3

import timeimport threadingdef calc_square(number):   print("Calculate square numbers: ")   for i in numbers:      time.sleep(0.2)   #artificial time-delay      print('square: ', str(n*n))def calc_cude(number):   print("Calculate cude numbers: ")   for i in numbers:      time.sleep(0.2)      print('cube: ', str(n*n*n))arr = [2,3,8,9]t = time.time()t1 = threading.Thread(target = cal_square,args=(arr,))t2 = threading.Thread(target = cal_cube,args=(arr,))# creating two threads here t1 & t2t1.start()t2.start()# starting threads here parallelly by usign start function.t1.join()# this join() will wait until the cal_square() function is finised.t2.join()# this join() will wait unit the cal_cube() function is finised.print("Successed!")