Multithreading Hinglish BlackBG
Multithreading Hinglish BlackBG
Multithreading ek programming concept hai jisme ek single process ke andar multiple threads ko run kiya jata
hai simultaneously.
Thread ek lightweight sub-process hoti hai jo independently run kar sakti hai, lekin uska control ek hi process
ke paas hota hai.
Multithreading ka use aise tasks mein hota hai jahan hum chahte hain ki multiple kaam ek hi samay par ho
sakein.
Fayde:
threading module se hum thread create karke function ko alag thread mein run kara sakte hain.
Example:
import threading
def print_numbers():
for i in range(5):
print('Number:', i)
t1 = threading.Thread(target=print_numbers)
t1.start()
print('Main thread continues...')
Is method mein hum Thread class ko inherit karke run method define karte hain.
Example:
import threading
class MyThread(threading.Thread):
Multithreading in Python (Hinglish)
def run(self):
for i in range(3):
print('Running thread:', i)
t = MyThread()
t.start()
3. Using concurrent.futures
Modern method jisme thread pool banakar thread execute karte hain.
Example:
from concurrent.futures import ThreadPoolExecutor
def greet(name):
print('Hello', name)
Note:
Python mein GIL (Global Interpreter Lock) ek time pe sirf ek thread ko CPU access deta hai.
Multithreading I/O-bound tasks ke liye useful hai, CPU-bound ke liye nahi.
Conclusion:
Multithreading ek powerful technique hai jo program ko fast aur efficient banati hai.
Python mein hum threading module, Thread class inheritance, aur concurrent.futures ka use karke
multithreading kar sakte hain.