0% found this document useful (0 votes)
8 views2 pages

Multithreading Hinglish BlackBG

Multithreading in Python allows multiple threads to run simultaneously within a single process, enhancing CPU utilization and execution speed. It can be implemented using the threading module, by extending the Thread class, or through the concurrent.futures module for modern thread management. However, due to Python's Global Interpreter Lock (GIL), multithreading is more effective for I/O-bound tasks rather than CPU-bound tasks.

Uploaded by

deependras7410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Multithreading Hinglish BlackBG

Multithreading in Python allows multiple threads to run simultaneously within a single process, enhancing CPU utilization and execution speed. It can be implemented using the threading module, by extending the Thread class, or through the concurrent.futures module for modern thread management. However, due to Python's Global Interpreter Lock (GIL), multithreading is more effective for I/O-bound tasks rather than CPU-bound tasks.

Uploaded by

deependras7410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Multithreading in Python (Hinglish)

Multithreading Kya Hai?

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:

1. Better CPU utilization


2. Faster execution
3. Efficient program design
4. Resource sharing

1. Using threading module

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

2. Extending the Thread class

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)

with ThreadPoolExecutor() as executor:


names = ['Alice', 'Bob', 'Charlie']
executor.map(greet, names)

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.

You might also like