Multi treads in python

# Multi treading 
# flow of execution. like a separate order of instructions
# however each thread takes a turn running to achieve concurrecny
# GIL = (globals interpreter lock).
# allows only one threaad to hold the control of the pythong interpreter
# cpu bound  = pr4ogram/task spreads most of it's time waiting for internel events (CPU intensive)
#             ( use mulitprocessing )
        
# IO bound = program/task spends most of it's time waiting for external events 
# (user intputs ,web scraping)
#             (use mutlitreading)

import threading 
import time
from threading import Thread
from gtts import gTTS
import playsound


def speak(text,):
        tts = gTTS(text=text, lang="en")
        file = "voice.mp3"
        tts.save(file)
        playsound.playsound(file)

def speak1(text,):
        tts = gTTS(text=text, lang="en")
        file = "voice2.mp3"
        tts.save(file)
        playsound.playsound(file)


def speak2(text,):
        tts = gTTS(text=text, lang="en")
        file = "voice1.mp3"
        tts.save(file)
        playsound.playsound(file)


def eat_breakfast():
    time.sleep(3)
    print("You finished your break fast, good")
    speak("You finished your break fast,and thats good")
    
def drink_coffee():
    time.sleep(4)
    print("you finished your coiffee, Incredible")
    speak1("you finished your coiffee,and thats incredible")

def study():
    time.sleep(5)
    print("You finished your homework,Awesome")
    speak2("You finished your homework,and thats Awesome")
= Thread(target=eat_breakfast,args=())
x.start()
= Thread(target=drink_coffee)
y.start()
= threading.Thread(target=study)
z.start()



x.join()
y.join()
z.join()

print(threading.active_count())
print(threading.enumerate())
print(time.perf_counter())




Comments